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ı");
}
}
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){
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With