我只是想在我的應用程式中處理一個集合后將它保存到資料庫中。
$data = SomeModel::all();
$data = DataProcessor::process($data);
//Now I want to save the result back to the database...
//This doesn't work since it's a collection so you get the error:
//"Method Illuminate\Database\Eloquent\Collection::save does not exist."
$data ->save();
//This works but is not feasible due to the amount (thousands) of records needed to be updated
foreach($data as $dat){
$dat->save();
}
//This doesn't work because I'm updating, not inserting new records.
//So I get integrity constraint violation on id
SomeModel::insert($data);
我覺得這應該是一個非常簡單的解決方案的常見做法,但我一直在環顧四周,沒有找到任何東西。
uj5u.com熱心網友回復:
根據檔案,您可以嘗試 upserts:https ://laravel.com/docs/8.x/eloquent#upserts
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403661.html
標籤:
