問題描述
我不知道我做錯了什么,因為它適用于應用程序中的所有其他模型.我多次刷新并重新播種數據庫.這些模型擴展了相同的抽象方法.
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. The models extend the same abstract methods.
這是控制器中的代碼:
$substrates = $this->substrates->all()->toArray();
$temp = json_encode($substrates);
dd($temp, json_last_error(), json_last_error_msg(), $substrates);
這是 dd() 輸出:
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 [?] ....
當我使用 JSON_PARTIAL_OUTPUT_ON_ERROR 時,我得到一個 json 字符串.
When I use JSON_PARTIAL_OUTPUT_ON_ERROR I get a json string.
推薦答案
錯誤的原因是你在 pic
中存儲了一個 stream 資源無法序列化為 JSON 的序列化對象的字段.
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.
您可以通過在模型中設置 $hidden
屬性來告訴 Eloquent 模型在將所選屬性轉換為數組時跳過它們:
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'];
}
這篇關于無法 json_encode() 數組或 Laravel 集合:“不支持類型"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!