'Laravel how to modify the returned result from eloquent to convert json string to json object
Description and welcomeEmailContents are type text in db ,and both store same type data . Description return json obj while issue is welcomeEmailContents return string.
$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
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
