Laravel The Response content must be a string or object implementing __toString(), "object" given Laravel The Response content must be a string or object implementing __toString(), "object" given json json

Laravel The Response content must be a string or object implementing __toString(), "object" given


When you do

return \App\User::first()->skills();

you are returning the Relation definition object, which doesn't implement __toString() method. What you need in order to return the related object is

return \App\User::first()->skills;

This will return a Collection object cotaining related skills - this will be properly serialized.


May be you could go with get_object_vars($skills) and later loop through the object variables. Example:

foreach(get_object_vars($skills) as $prop => $val){}


in the Route.php, return a collection like so

Route::get('setting',function(){     return \App\User::first()->skills;});

OR

Route::get('setting',function(){    $skills = \App\User::first(['id','skills']);    return $skills->skills;});

And on you User.php model, you can factor your code say so

protected $casts = [    'skills' => 'json'];public function skills(){    return $this->skills;}

I hope this helps someone.