Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5 在播种机加载雄辩数据时发生数组合并错误 提要_Laravel 5_Eloquent_Php - Fatal编程技术网

Laravel 5 在播种机加载雄辩数据时发生数组合并错误 提要

Laravel 5 在播种机加载雄辩数据时发生数组合并错误 提要,laravel-5,eloquent,php,Laravel 5,Eloquent,Php,我遇到了一个非常令人困惑的问题。基本上,我有很多种子机,它们从JSON文档加载数据,并使用插入到数据库中—这很好用 我有一个最终的播种机,它循环遍历一些数据,在此循环期间,我使用加载一个记录,但当我尝试访问数据时,我收到以下错误: [ErrorException] array_merge(): Argument #1 is not an array 播种机代码段 这个错误令人困惑的地方在于,如果我不使用雄辩的语言,我的问题就解

我遇到了一个非常令人困惑的问题。基本上,我有很多种子机,它们从JSON文档加载数据,并使用插入到数据库中—这很好用

我有一个最终的播种机,它循环遍历一些数据,在此循环期间,我使用加载一个记录,但当我尝试访问数据时,我收到以下错误:

  [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);
}