尝试将信息添加到空的 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 已经有信息那么这个问题就不会发生并且代码可以正常工作。
最佳答案
你可以这样做:
if (count($user->profile))
{
$user->profile->fill($input)->save();
}
else
{
$profile = Profile::create($input);
$user->profile()->save($profile);
}
关于php - 调用非对象的成员函数 fill(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034176/