How to update a collection using Eloquent Laravel
I know it solve the problem but answering for new users that how they can update the laravel model collection
Simply add an update on the collection
Mail::where('to', auth()->user()->email)->update([
'is_seen' => 1
]);
Since $commands
is a collection, changing the value of $commands->status
will not have the effect that you intend (setting the value of status
to 'sent' for every item in the collection).
Instead, act on each item in the collection independently:
foreach ($commands as $command)
{
$command->status = 'sent';
$command->save();
}
You can also update the items in the database via Query Builder:
DB::table('your_table')->where('status', 'pending')->update(array('status' => 'pending'));
You could try the update
method on the \Illuminate\Database\Eloquent\Builder
object:
$queryBuilder = $device->commands()->whereStatus("pending");
$queryBuilder->update(array("status" => "sent"));