I have a one to many relationship between Device
and Command
models (each Device
has many commands
). Now I want to update a c
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"));