Laravel 5 在播种机加载雄辩数据时发生数组合并错误 提要
我遇到了一个非常令人困惑的问题。基本上,我有很多种子机,它们从JSON文档加载数据,并使用插入到数据库中—这很好用 我有一个最终的播种机,它循环遍历一些数据,在此循环期间,我使用加载一个记录,但当我尝试访问数据时,我收到以下错误:Laravel 5 在播种机加载雄辩数据时发生数组合并错误 提要,laravel-5,eloquent,php,Laravel 5,Eloquent,Php,我遇到了一个非常令人困惑的问题。基本上,我有很多种子机,它们从JSON文档加载数据,并使用插入到数据库中—这很好用 我有一个最终的播种机,它循环遍历一些数据,在此循环期间,我使用加载一个记录,但当我尝试访问数据时,我收到以下错误: [ErrorException] array_merge(): Argument #1 is not an array 播种机代码段 这个错误令人困惑的地方在于,如果我不使用雄辩的语言,我的问题就解
[ErrorException]
array_merge(): Argument #1 is not an array
播种机代码段
这个错误令人困惑的地方在于,如果我不使用雄辩的语言,我的问题就解决了,如下所示:
foreach ($data as $country => $states) {
$countryId = DB::table((new Country)->getTable())
->select('id')
->where('iso2_code', $country)
->first();
dd($countryId->id); // Works.
array_walk($states, function (&$value) use ($countryId) {
$value = ['country_id' => $countryId, 'name' => $value];
});
State::insert($states);
}
那么为什么DB
按预期工作,但雄辩
会触发一些数组合并错误?解决方案
在我的情况下,我在我的模型上设置了protected$dates=false
。其中,通常根据文档,我应该使用public$timestamps=false
或protected$dates=array()代码>
foreach ($data as $country => $states) {
$countryId = DB::table((new Country)->getTable())
->select('id')
->where('iso2_code', $country)
->first();
dd($countryId->id); // Works.
array_walk($states, function (&$value) use ($countryId) {
$value = ['country_id' => $countryId, 'name' => $value];
});
State::insert($states);
}