不支持json_encode()和Array或laravel集合,“不支持类型”

Mun*_*ber 2 php json laravel eloquent laravel-5

我不知道我在做什么错,因为它可以与应用程序中的所有其他模型一起使用。我刷新并重新播种了数据库多次。并且这些模型扩展了相同的抽象方法

这是控制器中的代码:

    $substrates = $this->substrates->all()->toArray();
    $temp = json_encode($substrates);
    dd($temp, json_last_error(), json_last_error_msg(), $substrates);
Run Code Online (Sandbox Code Playgroud)

这是dd()的输出:false 8“不支持类型”

数组: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"
Run Code Online (Sandbox Code Playgroud)

]

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

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

jed*_*ylo 5

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

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

class Substrate extends Model {
  protected $hidden = ['pic'];
}
Run Code Online (Sandbox Code Playgroud)