【问题标题】:array_merge error during seeder whilst loading eloquent data加载雄辩数据时播种器期间的array_merge错误
【发布时间】:2015-05-19 21:54:59
【问题描述】:

概要

遇到了一个非常令人困惑的问题。基本上我拥有的是一些播种机,它们从 JSON 文档加载数据并使用 插入数据库 - 这工作正常。

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

  [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


    【解决方案1】:

    解决方案

    在我的情况下,我在我的模型上设置了protected $dates = false。通常根据文档我应该使用public $timestamps = falseprotected $dates = array();

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 2020-01-21
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2017-05-16
      相关资源
      最近更新 更多