How can you update multiple models and their relations at the same time?
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()
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
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
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.