Home » Php » php – Laravel The Response content must be a string or object implementing __toString(), "object" given

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to run Skills function but I cant it.

Route.php

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

User.php

protected $casts = [
    'skills' => 'json'
];

public function skills(){
    return new Skills($this , $this->skills);
}

Skills.php

namespace App;
use App\User;
use Mockery\Exception;

class Skills
{
    protected $user;
    protected $skills = [];

    public function __construct(User $user,array $skills){

        $this->user=$user;
        $this->skills=$skills;
    }
}

I want to enter /settings page I have “The Response content must be a string or object implementing __toString(), "object" given.” error.

I tried to add dd() function’s return in route, I see all JSON data but $skills->get(), $skill->set() didn’t working at the time.

Edit:

Skills.php

<?php
    /**
     * Created by PhpStorm.
     * User: root
     * Date: 01.08.2015
     * Time: 11:45
     */

    namespace App;
    use App\User;
    use Mockery\Exception;

    class Skills
    {
        protected $user;
        protected $skills = [];

        public function __construct(User $user,array $skills){
            $this->user=$user;
            $this->skills=$skills;
        }

        public function get($key){
            return array_get($this->skills,$key);
        }

        public function set($key,$value){
            $this->skills[$key]=$value;
            return $this->duration();
        }

        public function has($key){
            return array_key_exists($key,$this->skills);
        }

        public function all(){
           return $this->skills;
        }

        public function merge(array $attributes){
            $this->skills = array_merge(
                $this->skills,
                array_only(
                    $attributes,
                    array_keys($this->skills)
                )
            );
            return $this->duration();
        }

        public function duration(){
            return $this->user->update(['skills' => $this->skills]);
        }

        public function __get($key){
            if ($this->has($key)) {
                return $this->get($key);
            }
            throw new Exception("{$key} adlı Yetenek bulunamadı");
        }
    }
How to&Answers:

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.

Answer:

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){
}

Answer:

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.