2015-08-02 10 views
8

Voglio eseguire la funzione Abilità ma non posso.Laravel Il contenuto della risposta deve essere una stringa o un oggetto che implementa __toString(), "oggetto" dato

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; 
    } 
} 

voglio accedere alla pagina/impostazioni ho "The Response content must be a string or object implementing __toString(), "object" given." errore.

Ho provato ad aggiungere il ritorno dd() di funzione nel percorso, vedo tutti i dati JSON ma $skills->get(), $skill->set() non ha lavoro al momento.

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

risposta

6

Quando si esegue

return \App\User::first()->skills(); 

si restituisce l'oggetto di definizione Relazione, che non implementa __toString() metodo. Quello che vi serve per restituire l'oggetto correlato è

return \App\User::first()->skills; 

Questo restituirà un Collection oggetto cotaining competenze relative - questo sarà correttamente serializzato.

+0

Grazie per risposta sto cercando ma voglio correre App \ User \ :: first() -> competenze() -> set ("HTML", "100"); return \ App \ User :: first() -> skills; İts non impostato :) –

+0

E cosa dovrebbe fare quella funzione set()? Quale versione di Laravel stai usando? Non riesco a vedere nulla di simile nel codice di Laravel –

+0

Sto condividendo i codici, sto usando laravel 5 –

1

Potrebbe essere possibile utilizzare get_object_vars($skills) e successivamente loop attraverso le variabili dell'oggetto. Esempio:

foreach(get_object_vars($skills) as $prop => $val){ 
} 
0

nel Route.php, restituire un insieme in questo modo

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

O

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

E del modello User.php, è possibile fattore tua codice dire così

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

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

Spero che questo aiuti qualcuno.

Problemi correlati