如何使用 laravel 中的两个键更新一行?
How can i update one row usign two keys in laravel?
我需要像这样更新 table :
UPDATE table SET column = value WHERE first_id = 1 AND second_id = 2
是否可以使用 eloquent 模型在 Laravel 中执行此操作?如果是我该怎么做?
我正在尝试:
$object = Model::where('first_id' , 1)->where('second_id' , 2)->get();
$object->column = $value
$object->save();
是的,你可以做到。以下是您的操作方法。
Model::where('first_id', 1)
->where('second_id', 2)
->update(['column' => 'yourValue']);
您正在获取一个集合。您可以更新每个集合的列。您只能使用 first 获取第一个集合。
foreach($object as $obj){
$obj->column = $value;
$obj->save();
}
或者
$object = Model::where('first_id' , 1)->where('second_id' , 2)->first();
$obect->column =$value;
$object->save();
我需要像这样更新 table :
UPDATE table SET column = value WHERE first_id = 1 AND second_id = 2
是否可以使用 eloquent 模型在 Laravel 中执行此操作?如果是我该怎么做?
我正在尝试:
$object = Model::where('first_id' , 1)->where('second_id' , 2)->get();
$object->column = $value
$object->save();
是的,你可以做到。以下是您的操作方法。
Model::where('first_id', 1)
->where('second_id', 2)
->update(['column' => 'yourValue']);
您正在获取一个集合。您可以更新每个集合的列。您只能使用 first 获取第一个集合。
foreach($object as $obj){
$obj->column = $value;
$obj->save();
}
或者
$object = Model::where('first_id' , 1)->where('second_id' , 2)->first();
$obect->column =$value;
$object->save();