php – 在非对象上调用成员函数fill()

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在非对象上调用成员函数fill()脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试将信息添加到空配置文件重定向回来.

$user = User::whereUsername($username)->FirstOrFail(); // Selects correct user

$input = Input::all(); // a dd($input) at this point confirms input PResent

$this->profileForm->validate($input); // Passes

$user->profile->fill($input)->save();

return redirect::route('profile.edIT',$user->username);

如果$user-> profile为null,则会出现错误调用非对象上的成员函数fill().我尝试用以下方法解决这个问题:

$user = User::whereUsername($username)->firstOrFail(); // Selects correct user

$input = Input::all(); // a dd($input) at this point confirms input present

$this->profileForm->validate($input); // Passes

if ($user->profile == null)
{
    $user->profile = new Profile;
}

$user->profile->fill($input)->save();

return Redirect::route('profile.edit',$user->username);

在这种情况下,它会重定向而不添加配置文件详细信息(此时$user->配置文件仍为null).

如果$user->配置文件已有信息,则不会发生此问题,并且代码正常工作.

解决方法

你可以这样做:

if (count($user->profile))
{   
    $user->profile->fill($input)->save();
}
else
{
    $profile = Profile::create($input);

    $user->profile()->save($profile);
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在非对象上调用成员函数fill()全部内容,希望文章能够帮你解决php – 在非对象上调用成员函数fill()所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。