Laravel Eloquent retrieve row with JSON column Laravel Eloquent retrieve row with JSON column laravel laravel

Laravel Eloquent retrieve row with JSON column


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"}}