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

FarhadMohammadi's avatar

How to return collectin after update on laravel eloquent?

I have a batch update query, I need to get updated records for filter some of them to dispaching related jobs. The query is:

	$onlineRows = Order::toDoneBy(Order::PAY_TYPE_ONLINE)
        ->update(['status' => Order::STATUS_DONE])
        ->get()
        ->each(fn($order) => ResigterDoctorTransaction::dispatch($order))
        ->pluck('id');

but we have an error on this query, call get() on int cuz update return number of affected rows, How do I do? by the way scope toDoneBy is return multi records based on some where

0 likes
4 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to do a separate query. You cannot do a query after ->update()

How about

$onlineRows = Order::toDoneBy(Order::PAY_TYPE_ONLINE)->get();

Order::toDoneBy(Order::PAY_TYPE_ONLINE)
        ->update(['status' => Order::STATUS_DONE]);

$onlineRows =  $onlineRows->each(fn($order) => ResigterDoctorTransaction::dispatch($order))
        ->pluck('id');
1 like
Sinnbeck's avatar

Or

$ids = Order::toDoneBy(Order::PAY_TYPE_ONLINE)->pluck('id');

Order::toDoneBy(Order::PAY_TYPE_ONLINE)
        ->update(['status' => Order::STATUS_DONE]);

$onlineRows =  Order::whereIn('id', $ids)->each(fn($order) => ResigterDoctorTransaction::dispatch($order))
        ->pluck('id');
1 like
FarhadMohammadi's avatar

@Sinnbeck ya exactly, the first solution I found was that so I thought there is another way to avoid two query, but you right there no way to did by single query

Sinnbeck's avatar

@FarhadMohammadi Sadly no. Mysql has no way of returning the ids of the rows being updated. So until that gets added to mysql, laravel cannot do so either

1 like

Please or to participate in this conversation.