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

30,820

Solution 1

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.

Solution 2

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){
}
Share:
30,820
Alameddin Çelik
Author by

Alameddin Çelik

Updated on September 19, 2020

Comments

  • Alameddin Çelik
    Alameddin Çelik over 3 years

    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ı");
            }
        }
    
  • Alameddin Çelik
    Alameddin Çelik over 8 years
    Thanks for Answer I'm trying but I want to run \App\User::first()->skills()->set("HTML","100"); return \App\User::first()->skills; İts not set :)
  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    And what is that set() function supposed to do? What version of Laravel are you using? I cannot see anything like that in the Laravel's code
  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    Oh, I thought skills() is a one-to-many relation between User and Skills, not some custom function
  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    Ok, 2 things. If you want to return Skills object from the controller, it needs to implement __toString() method - it doesn't and that's why you're getting the error. And what you're doing in your code is very unusual with Laravel - you seem to implement some kind of relation but do this yourself instead of using Eloquent's functionality. Have a look at the docs to see how to define relationships between models: laravel.com/docs/5.1/eloquent-relationships