我只想在应用程序中处理集合后将其保存到数据库中。
$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 IlluminateDatabaseEloquentCollection::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);
我觉得这应该是一种常见的做法,有一个非常简单的解决方案,但我一直在四处寻找,没有发现任何东西。
根据文档,您可以尝试upserts:https://laravel.com/docs/8.x/eloquent#upserts