問題描述
參考:如何更新Laravel 4 中現有的 Eloquent 關系?
$userinfo = Userinfo::find($id);
User::find($id)->userinfo()->associate($userinfo)->save();
我收到錯誤:Call to undefined method IlluminateDatabaseQueryBuilder::associate()
這里是整個方法:
public function saveUser($id)
{
$user = User::find($id);
$userdata = Input::all();
$rules = array(
'email' => 'required|email',
'state' => 'size:2',
'zip' => 'size:5',
'phone' => array('regex:/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/')
);
$validator = Validator::make($userdata, $rules);
if ($validator->passes())
{
if ($userdata['email'] !== $user->email)
{
$rules = array('email' => 'unique:users');
$validator = Validator::make($userdata, $rules);
if ($validator->fails()) return Redirect::route('admin.user.edit', array('user' => $user))
->with('error', 'Specified email already exists.');
}
$user->email = $userdata['email'];
$user->firstname = $userdata['firstname'];
$user->lastname = $userdata['lastname'];
$userinfoArray = array(
'address' => $userdata['address'],
'city' => $userdata['city'],
'state' => $userdata['state'],
'zip' => $userdata['zip'],
'phone' => preg_replace('/[^0-9]/', '', $userdata['phone'])
);
$user->save();
if (!$user->userinfo)
{
$userinfo = new Userinfo($userinfoArray);
$userinfo = $user->userinfo()->save($userinfo);
}
else
{
$userinfo = Userinfo::find($id);
User::find($id)->userinfo()->associate($userinfo)->save();
//$user->userinfo()->update($userinfoArray);
}
return Redirect::route('admin.user.detail', array('id' => $id))
->with('success', 'User updated.');
}
return Redirect::route('admin.user.edit', array('id' => $id))
->withInput()
->withErrors($validator);
}
推薦答案
associate() 是belongsTo 關系的一個方法,但從上面看來,您試圖通過hasOne 關系調用它.
associate() is a method of the belongsTo relationship, but it looks like from the above you are trying to call it via the hasOne relationship.
我只是猜測,因為您沒有提供您雄辯的模型類代碼,所以無法看到您是如何準確設置關系的,但如果您有:
I am just guessing as you have not provided your eloquent model class code so can't see how you have set the relationships exactly, but if you have:
class User extends Eloquent {
public function userinfo()
{
return $this->hasOne('Userinfo');
}
}
class Userinfo extends Eloquent {
public function user() {
return $this->belongsTo('User');
}
}
然后需要針對 Userinfo 調用關聯,因為它具有關聯 () 方法附加到的belongsTo 關系.
Then associate needs to be called against Userinfo as this has the belongsTo relationship to which the associate() method is attached.
例如
$user = User::find(4);
$userinfo = UserInfo::find(1);
$userinfo->user()->associate($user);
$userinfo->save();
將user_info表中的外鍵user_id設置為$user對象的id.
Will set the foreign key user_id in the user_info table to the id of the $user object.
看看你上面的代碼,這似乎不是你真正想要做的,
Looking at your above code it doesn't appear that this is what you are actually trying to do and that the
$user->userinfo()->update($userinfoArray);
您注釋掉的調用實際上會執行您似乎想要實現的目標,即更新與當前用戶相關的用戶信息(如果該用戶已存在).
call which you have commented out will in fact do what you seem to be trying to achieve, which is to update the userinfo that is related to the current user if that user already exists.
希望這會有所幫助.
格倫
這篇關于調用未定義的方法 IlluminateDatabaseQueryBuilder::associate()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!