Reputation: 8426
How can you update multiple models and their relations at the same time?
For example:
EditPost
is a model with a editor()
relation belongsTo
User
model.
Now lets say I have to update the editor in all the EditPost
objects with original_post_id
EditPost::where('original_post_id',4)->get()
Possible Solutions
a. To do it by referring the user by ID instead of by the Model User
EditPost::where('original_post_id',4)->update(array('editor_id',3));
b. To do it by a foreach
and saving each model
However
Neither of these appeals to me as they don't gel in general with the object concept of Eloquent or they would mean doing multiple updates instead of one. I was wondering if Eloquent itself had a more elegant solution
Upvotes: 0
Views: 5346
Reputation: 7450
You don't specify the other end of the association, but I assume you are looking for something like this?:
$user = User::find(3)
EditPost::where('original_post_id', 4)->editor()->associate($user)->save();
Upvotes: 2