【发布时间】:2014-09-25 08:44:38
【问题描述】:
尝试将信息添加到空的profile 并重定向回来。
$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。我试图解决这个问题:
$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)。
如果 $user->profile 已经有信息,则不会出现此问题,并且代码可以正常工作。
【问题讨论】:
-
你在调用
fill()之前初始化新的配置文件吗? -
是的,在
fill()之前添加了if子句。我已经编辑了代码。