在非对象上调用成员函数fill() [英] Call to a member function fill() on a non-object
本文介绍了在非对象上调用成员函数fill()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试将信息添加到空的profile
并重定向回.
Trying to add information to an empty profile
and redirect back.
$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
,则出现错误:Call to a member function fill() on a non-object
.我试图用以下方法解决这个问题:
If $user->profile
is null
then this gives the error: Call to a member function fill() on a non-object
. I tried to remedy this with:
$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->profile
仍为null
).
But in this case it is redirected without adding the profile details ($user->profile
is still null
at this point).
如果$ user-> profile文件已经包含信息,则不会出现此问题,并且代码可以正常工作.
If $user->profile already has information then this problem does not occur and the code works fine.
推荐答案
您可以这样做:
if (count($user->profile))
{
$user->profile->fill($input)->save();
}
else
{
$profile = Profile::create($input);
$user->profile()->save($profile);
}
这篇关于在非对象上调用成员函数fill()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文