dpz1983 2018-01-28 13:45
浏览 252
已采纳

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

I don't know what I'm doing wrong since it works with all the other models in the app. I refreshed and reseeded the database multiple times. And the models extend the same abstract methods

This is the code in the controller:

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

This is the dd() output: 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 [▶] ....

When I use JSON_PARTIAL_OUTPUT_ON_ERROR I get a json string.

  • 写回答

1条回答 默认 最新

  • dongraa1986 2018-01-29 11:00
    关注

    The reason for the error is the fact, that you're storing a stream resource in pic field of the serialised object that can't be serialised to JSON.

    You can tell Eloquent model to skip selected attributes when they're converted to an array by setting a $hidden attribute in your model:

    class Substrate extends Model {
      protected $hidden = ['pic'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料