prgrm
prgrm

Reputation: 3833

Setting all values of table in Laravel

I want to set every value of a table at the same time. It is something like this:

$items = Item::where("user_id",$user_id)->get();

foreach($items as $item){
$item->propiety = 5;
$item->save();
}

This works but I wonder if there is a better way to do it with Eloquent, as I can think better ways to do it with SQL.

Upvotes: 0

Views: 33

Answers (1)

The Alpha
The Alpha

Reputation: 146201

You may do it this way:

Item::where("user_id", $user_id)->update(['propiety' => 5]);

Upvotes: 2

Related Questions