The problem is that min
will return a number and not an actual model. If you want the actual model which has the min value you need to something like:
Orders::where('user_id', '!=', $user_id)
->where('odm_id', $odm_id)
->wherePriority(function ($query) use ($user_id, $odm_id) {
$query->from(\DB::raw('(SELECT * FROM orders) AS o'))
->selectRaw("MIN(`o`.`priority`)")
->where('o.odm_id', $odm_id)
->where('o.expired', 0);
})->update(['priority' => 1]);
Note that this will update all records which have expired 0, odm_id
equal to $odm_id
and match the minimum value.
Alternatively you can do:
$orders =Orders::where('user_id', '!=', $user_id)
->where('odm_id', $odm_id)
->wherePriority(function ($query) use ($user_id, $odm_id) {
$query->from(\DB::raw('(SELECT * FROM orders) AS o'))
->selectRaw("MIN(`o`.`priority`)")
->where('o.odm_id', $odm_id)
->where('o.expired', 0);
})->get();
$orders->each(function ($order) {
$order->priority = 1;
$order->save();
});
The second way has the disadvantage of doing 2 separate queries, one to get the data and one to update, however this has the advantage of triggering model events such as saving etc.