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

eloquent, laravel, laravel-4

Solution

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.

Problem

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!

Original source