Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Eloquent how to update a model and related models in one go

Does anyone know if it is possitble to do the folowing:

Let's say we have a model called User and a model calledd BestFriend. The relation between the User and the best friend is 1:1.

I would like for these cases be able to do something like this, change my city and the city of my friend at the same time.

$me = User::find(1);  $me->update(array( 'city' => 'amsterdam', 'bestfriend.city' => 'amsterdam' )); 

So basically I would like to know if Eloquent is smart enough to understand the relationship based on the array key 'bestfriend.city'.

Thanks in advance for any help!

Update:

Found the solution on the Laravel forums but im posting it here as well if someone else is looking for the same thing :)

In the model you add

// In your model... public function setBestFriendArrayAttribute($values) {     $this->bestfriend->update($values); } 

And then you can call it like this

$me->update(array(     'city' => 'amsterdam',     'BestFriendArray' => array(         'city' => 'amsterdam'     ) )); 

Works like a charm!

like image 866
MvdP Avatar asked Jul 30 '13 14:07

MvdP


People also ask

What is polymorphic relationship Laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.

What is with () in Laravel?

with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.

What is belongsTo in Laravel?

BelongsTo relationship in laravel is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use belongsTo relationship.


2 Answers

You don't need to set it on your model. You can do it on your controller like this.

$me = User::find(1)->bestFriend()->update(array( 'city' => 'amsterdam', 'bestfriend.city' => 'amsterdam' )); 

I just modified your update a little bit.

like image 189
dakine Avatar answered Oct 02 '22 13:10

dakine


Eloquent is pretty smart, but I don't believe it can do that. You would have to update User and BestFriend independently. But once you've done that, Eloquent does have methods for attaching the two.

$me = User::find(1); $bff= BestFriend::find(1);  $me->city = 'amsterdam';  $bff->city = 'amsterdam';  $me->bestfriend()->associate($bff); 

This is of course assuming your User model has a function that looks like...

public function bestfriend() {     return $this->hasOne('BestFriend'); } 
like image 45
user1669496 Avatar answered Oct 02 '22 13:10

user1669496