不能json_encode()一个数组或Laravel集合: "不支持类型"

问题描述 投票:0回答:0

我不知道我做错了什么,因为它与应用程序中的所有其他模型一起工作。我多次刷新和重新加载数据库。这些模型扩展了相同的抽象方法。

这是控制器中的代码。

$substrates = $this->substrates->all()->toArray();
$temp = json_encode($substrates);
dd($temp, json_last_error(), json_last_error_msg(), $substrates);

这是dd()的输出

false
8
"Type is not supported"

array:119 [▼

  0 => array:21 [▼

    "id" => 1
    "name" => "Wood Free"
    "machine_id" => 2
    "classification" => "Cover"
    "origins" => "Coming Soon"
    "duplex" => true
    "color" => "Translucents"
    "texture" => "Leather"
    "finish" => "Felt"
    "product_type" => "Sheet"
    "caliper" => "0.06"
    "m_weight" => 70
    "width" => "46.40"
    "height" => "32.00"
    "pic" => stream resource @17 ▶}
    "price" => "0.30"
    "created_by" => 38
    "updated_by" => 16
    "deleted_at" => null
    "created_at" => "2018-01-27 08:00:11"
    "updated_at" => "2018-01-27 08:00:11"
  ]

  1 => array:21 [▶] ....

当我使用JSON_PARTIAL_OUTPUT_ON_ERROR时,我得到一个json字符串。

php json zend-framework server
5
投票

出错的原因是,你的网站上存储了一个 溪流资源pic 的字段,不能被序列化为JSON。

您可以通过设置一个 "跳过 "选项来告诉 Eloquent 模型在将选定的属性转换为数组时跳过它们。$hidden 在你的模型中的属性。

class Substrate extends Model {
  protected $hidden = ['pic'];
}
© www.soinside.com 2019 - 2024. All rights reserved.