Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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
Php -&燃气轮机;toArray()提供的是空数组而不是对象_Php_Arrays_Laravel_Collections - Fatal编程技术网

Php -&燃气轮机;toArray()提供的是空数组而不是对象

Php -&燃气轮机;toArray()提供的是空数组而不是对象,php,arrays,laravel,collections,Php,Arrays,Laravel,Collections,我有一个包含1000个原始对象的分块数组: class Raw extends Model { protected $table = 'raws'; protected $casts = [ 'time' => 'datetime' ]; public $timestamps = [ "time" ]; private string $operationId; private st

我有一个包含1000个原始对象的分块数组:

class Raw extends Model
{

    protected $table = 'raws';
    protected $casts = [
        'time' => 'datetime'
    ];
    public $timestamps = [
        "time"
    ];
    private string $operationId;
    private string $meterId;
    private string $consoProd;
    private string $timestep;
    private string $unit;
    private string $timestamp;
    private string $value;
}

collect($measures)->chunk(1000)->each(function ($raws) use ($meter) {
            $raws = $raws->map(function ($raw) use ($meter) {
                return new \App\Models\Raw($meter['OperationID'], $meter['ID'], $meter['ConsoProd'], $meter['Timestep'], $meter['Unit'], $raw['t'], $raw['d']);
            })->toArray();
            dd($raws);
        });
当我不使用->toArray()时,我可以看到1000个原始元素的集合:

Illuminate\Support\Collection {#2857 ▼
  #items: array:1000 [▼
    0 => App\Models\Raw {#1857 ▼
      #table: "raws"
      #casts: array:1 [ …1]
      +timestamps: array:1 [ …1]
      -operationId: "ACC00000001"
      -meterId: "30002431650000"
      -consoProd: "Conso"
      -timestep: "600000000000"
      -unit: "kW"
      -timestamp: "2018-09-11T00:00:00+02:00"
      -value: "7"
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: false
      +wasRecentlyCreated: false
      #attributes: []
      #original: []
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [ …1]
    }
    1 => App\Models\Raw {#1858 ▶}
    2 => App\Models\Raw {#1859 ▶}
    3 => App\Models\Raw {#1860 ▶}
但我尝试将其转换为具有以下内容的数组:

dd($raws->toArray());
我得到:

array:1000 [
  0 => []
  1 => []
  2 => []
  3 => []
  4 => []
  5 => []
  6 => []
  7 => []
  8 => []
  9 => []...
为什么???

正如您在中所看到的,在它的构造函数中,它接受一个属性数组,而不是多个参数。因此,您需要像这样创建
Raw.php
模型

$raws = $raws->map(function ($raw) use ($meter) {
    return new \App\Models\Raw([
        'operationId' => $meter['OperationID'],
        'meterId' => $meter['ID'],
        'consoProd' => $meter['ConsoProd'],
        'timestep' => $meter['Timestep'],
        'unit' => $meter['Unit'],
        'timestamp' => $meter['t'],
        'value' => $meter['d'],
    ]);
})->toArray();

很难假设数据库列(数组中的键)必须是数据库的列名,因此请检查它。

我认为这是因为它们没有被保留,并且转换到未保存实体的数组时出现了这种行为。。需要更深入地了解完整答案,多亏了你的评论,我才能解决它!在我的模型中,我定义了字段:
private string$operationId…我试图将其更改为public,但无效。删除它们解决了问题。如果你能解释,我会接受你的回答!无论如何谢谢你!!!如评论中所述,我不会在模型中定义字段。