Chcę uruchomić funkcję Skills, ale nie mogę jej użyć.Laravel Treść odpowiedzi musi być ciągiem znaków lub obiektem implementującym __toString(), "obiekt" pod numerem
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;
}
}
chcę wejść na stronę/ustawienia mam "The Response content must be a string or object implementing __toString(), "object" given.
" błąd.
Próbowałem dodać powrót funkcji dd()
w trasie, widzę wszystkie dane JSON, ale $skills->get()
, $skill->set()
nie działa w tym czasie.
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ı");
}
}
Dzięki za Odpowiedź Próbuję ale chcę uruchomić \ App \ użytkownik :: first() -> umiejętności() -> set ("Kopiuj", "100"); return \ App \ User :: first() -> skills; Nie ustawia się :) –
A co powinna zrobić funkcja set()? Jakiej wersji Laravel używasz? Nie widzę czegoś podobnego w kodzie Laravela –
Dzielę się kodami, używam laravel 5 –