Reference: How can I update an existing Eloquent relationship in Laravel 4?
$userinfo = \Userinfo::find($id);
\User::find($id)->userinfo()->associate($userinfo)->save();
I'm getting the error: Call to undefined method Illuminate\Database\Query\Builder::associate()
Here is the entire method:
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);
}
Change
hasOne
tobelongsTo
. It will look like:You need to specify the field related like this:
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:
Then associate needs to be called against Userinfo as this has the belongsTo relationship to which the associate() method is attached.
For example
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
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.
Hope this helps.
Glen
I was stuck on this problem for a few days and it ended up being quite simple to solve. I had created a folder called 'models' in my 'app' folder but I had forgotten to reconfigure my auth.php file.
This was my error.
I fixed it by opening the auth.php in the config folder and changing the following line to include my models folder.
fix:
Hope this helps!