将json字段强制转换为Array返回String Laravel 5.8

时间:2019-08-27 10:58:00

标签: laravel laravel-5.8

您好,我正在使用 Laravel 5.8 MYSQL 5.7

我有一个模型:

class Customer extends Model
{
  protected $casts = [
        'json_field_one' => 'array',
        'json_field_two' => 'array'
    ];
}

我正在进行单元测试,试图在其中更新字段:

$params = [
   "customer"=>[
      "json_field_one"=>json_encode($json_field_one),
      "json_field_two"=>json_encode($json_field_two),
    ]
];
$response = $this->makeJsonPostCall($route, $params);
$response->assertStatus(200);

它可以正常工作,但是当我尝试通过查找来检查属性时:

$customerUpdated = Customer::find(json_decode($response->getContent())->customer->id);

我看到我的字段不是数组,而是字符串,我想直接从Model中获得一个数组。

迁移文件为:

$table->json("json_field_one")->nullable();
$table->json("json_field_two")->nullable()->after("json_field_one");

0 个答案:

没有答案