【发布时间】:2015-05-19 21:54:59
【问题描述】:
概要
laravel-5 遇到了一个非常令人困惑的问题。基本上我拥有的是一些播种机,它们从 JSON 文档加载数据并使用 eloquent 插入数据库 - 这工作正常。
我有一个循环遍历一些数据的最终播种器,在此循环期间,我使用加载记录但当我尝试访问数据时收到以下错误:
[ErrorException]
array_merge(): Argument #1 is not an array
播种机片段
foreach ($data as $country => $states) {
$countryId = Country::where('iso2_code', $country)->first()->pluck('id');
dd($countryId); // ErrorException
array_walk($states, function (&$value) use ($countryId) {
$value = ['country_id' => $countryId, 'name' => $value];
});
State::insert($states);
}
这个错误令人困惑的部分是,如果我不使用 Eloquent,我的问题就解决了,如下所示:
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 可以正常工作,但Eloquent 会触发一些array_merge 错误?
【问题讨论】:
标签: laravel-5 eloquent php eloquent laravel-5