不能json_encode()数组或Laravel集合:"Type is not supported"



我不知道我做错了什么,因为它适用于应用程序中的所有其他模型。我多次刷新并重新设定了数据库种子。这些模型扩展了相同的抽象方法。

这是控制器中的代码:

$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 字符串。

错误的原因是,您将流资源存储在无法序列化为 JSON 的序列化对象的pic字段中。

您可以通过在模型中设置$hidden属性来告诉 Eloquent 模型在将所选属性转换为数组时跳过这些属性:

class Substrate extends Model {
  protected $hidden = ['pic'];
}

最新更新