I am trying to update the relationship of a one-to-many relationship in Laravel. Unfortunately I couldn't find any documentation for it. Can anyone help me?
This is what I have so far:
class Account extends Eloquent
{
public function users()
{
return $this->hasMany('User');
}
}
class User extends Eloquent
{
public function account()
{
return $this->belongsTo('Account');
}
}
Now I am trying to update the relationship from USER(1) > ACCOUNT(50) to USER(1) > ACCOUNT(99). How would I do this? I tried the following:
$account = Account::find(99);
User::find(1)->account()->save($account);
But that doesn't work :-( Any help deeply appreciated!!
UPDATE:
The following works:
$user = User::find(1);
$user->account_id = 99;
$user->save();
...but there MUST be a better solution like the one above, right?
It works in many-to-many relationships with both the save() and attach() method to update the relationship between tables (from both sides of the relationship). In one-to-many relationships the attach() method doesn't seem to be supported.
Taylor Otwell's official answer is the following:
I couldn't find the associate() method in the official docs. So if someone else is looking for a solution to this. Here you go!
Hey you can refer the laravel docs for the solution.
Link http://laravel.com/docs/4.2/eloquent#inserting-related-models
**
Try
My apologies, I didn't understand what was trying to be accomplished.
This is how i would have done it
My migrations
Then
My base models
Then
My Controllers - Updating user_details schema
My Controllers - creating user_details schema
And thats it for creating and updating new records using Laravel 4's belongsTo relationships