Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

movepixels's avatar

Sequential Database Query

Not sure if this is possible, but I think if it was that it would be a nice little hook. To call a save() or update() then do something all in one line.

$profile->photo()->where('avatar', true)->update([$field => false]);
$profile->photo()->where('id', '=', $id)->update([$field => true, 'published' => true]);

So for example right now I have to call this twice, update photo where its currently set to true to false THEN update a specific id to true

Is there a way to accomplish this with 1 query vs 2? Like below is the idea.

$profile->photo()->where($field, true)->update([$field => false])->then()->where('id', '=', $id)->update([$field => true, 'published' => true]);

Thanks

Dave

0 likes
2 replies
MilitaruC's avatar

// Geting the user
$profile = User::find(1); //the user_id
// Use User::model relation to Photo::model and save the photo options
$profile->photo()->update([$field => true, 'published' => true]);
movepixels's avatar

Thanks, but thats pretty much what I have posted. I already have the user object via route model binding.

I was hoping it was possible to set all $field = false for the user then update $field true where id = $id all in 1 single transaction

Please or to participate in this conversation.