Description 和 welcomeEmailContents 是 db 中的型別文本,并且都存盤相同的型別資料。描述回傳 json obj 而問題是welcomeEmailContents 回傳字串。
$course_desc_blocks = ($courseData['description'] != null) ? json_encode($courseData['description']) : null;
$course_welcome_blocks = ($courseData['welcomeEmailContents'] != null) ? json_encode($courseData['welcomeEmailContents']) : null;
//Laravel Query:-
$courseData = Course::select('id', 'title', 'description','welcomeEmailContents', 'price', 'site_id', 'status', 'expiry_days')->orderBy('id', 'asc'));
return response()->json( $courseData );
//=====================================
//output
{
"data": {
"id": 100,
"title": "Python",
"description": [
{
"type": "paragraph",
"data": {
"text": "jsonTesting"
}
}
],
"welcomeEmailContents": "[{\"type\":\"paragraph\",\"data\":{\"text\":\"Testingjson\"}}]",
"price": 0
}
}
uj5u.com熱心網友回復:
完成此操作的最簡單方法是在欄位中添加json演員表。welcomeEmailContents
class Course extends Model
{
protected $casts = [
'welcomeEmailContents' => 'json',
];
}
請注意,使用上面的代碼段,您不再需要在設定欄位時手動編碼 json,而只需將其設定為陣列。
Course::create([
// Some more fields...
'welcomeEmailContents' => [
[
'type' => 'paragraph',
'data' => [
'text' => 'testingjson',
],
],
],
]);
有多種方法可以完成上述操作。您可以在檔案中找到更多資訊:https ://laravel.com/docs/8.x/eloquent-mutators
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/421440.html
標籤:
上一篇:請我使用fetch從API獲取資料,但只有JSX沒有產品回傳。Api來自:http://fakestoreapi.com/docs
