I have following data in my table
id |name | option_data
1 |Joe |{"gender":"Male","cnic":"1234567","dob":"2016-03-14"}
2 |Doe |{"gender":"Male","cnic":"9999","dob":"2016-03-14"}
How can I get data in proper json_encode format.
For example if I write
echo json_encode($app->auth->users);
option_data":"{\"gender\":\"Male\",\"cnic\":\"61101-6859110-3\",\"dob\":\"2016-03-14\"}"
My database column type is Text
. If I remove all \\
using stripslashes still data is not a valid JSON because its wrapped in "
double qoutes.
How can I get data in proper json_encode format with all other coulmns.
You can just use $casts variable in your model and it will automatically convert to json and reverse to array.
protected $casts = [
"option_data" => "array"
];
Try with accesor in your model, to your data model
public function getOptionDataAttribute($value)
{
return json_decode($value);
}
and return in your controller:
return Model::all(); //What you need to retrieve
the output must be:
{
id: 1
name: name,
option_data: {"gender":"Male","cnic":"1234567","dob":"2016-03-14"}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.