我正在使用 PHP 和 Laravel 開發一個小專案,我嘗試更新我的模型,但收到一條錯誤訊息:
message "Non-static method Illuminate\\Database\\Eloquent\\Model::update() should not be called statically"
這是我的代碼:
AttributeOption::update(array_merge([
'sort_order' => $sortOrder ,
], $optionInputs), $optionId);
uj5u.com熱心網友回復:
你用update()錯了。
有條件的批量更新:
YourModel::where(/* some conditions */)
->update([
'field1' => 'value1',
'field2' => 'value2',
...
]);
無條件批量更新
YourModel::query()
->update([
'field1' => 'value1',
'field2' => 'value2',
...
]);
單一模型更新
$model = YourModel::where(/* some conditions */)->first();
$model->update([
'field1' => 'value1',
'field2' => 'value2',
...
]);
// Only accept fillable fields in the update
$model->fill([
'field1' => 'value1',
'field2' => 'value2',
...
])->save();
// Disregard fillable fields in the update
$model->forceFill([
'field1' => 'value1',
'field2' => 'value2',
...
])->save();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344425.html
