php – 在非对象上调用成员函数fill()
发布时间:2020-12-13 22:53:37 所属栏目:PHP教程 来源:网络整理
导读:尝试将信息添加到空配置文件并重定向回来. $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$u
尝试将信息添加到空配置文件并重定向回来.
$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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |