不能json_encode()和数组或laravel集合,"类型不受支持"

时间:2018-01-28 13:45:30

标签: php json laravel laravel-5 eloquent

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

这是控制器中的代码:

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

这是dd()输出: 假 8 "不支持类型"

阵列:119 [▼

0 =>阵列: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 =>阵列:21 [▶] ....

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

1 个答案:

答案 0 :(得分:4)

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

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

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