Alameddin Çelik
Alameddin Çelik

Reputation: 101

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

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ı");
        }
    }

Upvotes: 8

Views: 32145

Answers (3)

nensamuel
nensamuel

Reputation: 596

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.

Upvotes: 0

itsazzad
itsazzad

Reputation: 7277

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

Upvotes: 1

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40899

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.

Upvotes: 6

Related Questions