Eloquent 模型方案是否可以在数组下添加一个对象结构?

Is it possible to add an object structure under an array to the Eloquent model scheme?

我得到了一个这样的 JSON 对象:

{
   "campaignDesignerAccountId":0,
   "remoteAccountId":0,
   "month":5,
   "usage":[
      {
         "service":"affectivity",
         "type":"renderVideo",
         "taskCount":"59",
         "totalDuration":"2597.845625400543"
      },
      {
         "service":"creatives",
         "type":"HTMLToPDF",
         "taskCount":"248",
         "totalDuration":"5412.990473032003"
      },
      {
         "service":"creatives",
         "type":"pdf",
         "taskCount":"229",
         "totalDuration":"27953.42511272434"
      },
      {
         "service":"displayads",
         "type":"html5",
         "taskCount":"8",
         "totalDuration":"199.255334138869"
      }
   ]
}

我想将对象添加到 usage 数组下作为 eloquent 模型架构。

例如:

    protected $fillable = [
        'campaignDesignerAccountId',
        'remoteAccountId',
        '[usage].service',
        '[usage].type',
        '[usage].taskCount',
        '[usage].totalDuration',
        'month',

    ];

可能吗?

目前,我的 eloquent 模型如下所示:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PublishEngineMonthlyLogs extends Model
{

    protected $fillable = [
        'campaignDesignerAccountId',
        'remoteAccountId',
        'usage',
        'month'

    ];

    protected $casts = [
        'usage' => 'array'
    ];

    // Sorting parameters
    protected $allowedSorts = [
        'month'
    ];

    // Permission of filtering fields with texts
    protected $allowedFilters = [
        'month',
        'campaignDesignerAccountId'
    ];
}

我使用 mutators 解决了这个问题。

我像这样更新了我的模型 class:

class PublishEngineMonthlyLogs extends Model
{

    protected $fillable = [
        'campaignDesignerAccountId',
        'remoteAccountId',
        'usage',
        'month',
        'year'
    ];

    protected $casts = [
        'usage' => 'array'
    ];

    // Sorting parameters
    protected $allowedSorts = [
        'month'
    ];

    // Permission of filtering fields with texts
    protected $allowedFilters = [
        'month',
        'campaignDesignerAccountId'
    ];

    public function setServiceAttribute($value)
    {
        $this->attributes['service'] = $value;
    }
    public function setTaskCountAttribute($value)
    {
        $this->attributes['taskCount'] = $value;
    }
    public function setTotalDurationAttribute($value)
    {
        $this->attributes['totalDuration'] = $value;
    }
    public function setTypeAttribute($value)
    {
        $this->attributes['type'] = $value;
    }

}

并像这样分配值:

$tableData= [];
$log= new PublishEngineMonthlyLogs;
$log->setAttribute('service', $dataRow['service']);
$log->setAttribute('type', $dataRow['type']);
$log->setAttribute('taskCount', $dataRow['taskCount']);
$log->setAttribute('totalDuration', $dataRow['totalDuration']);
array_push($tableData, $log);