2013-06-04 13 views
8

Podczas korzystania z Eloquent ORM Laravel, nie mogę ustawić dynamicznie wartości $ hidden i $ visible w moim Modelu.Laravel/Eloquent - Chętnie ładowane ukryte/widoczne właściwości

Przykład 1: To działa:

class User extends Eloquent { 
    $this->visible = array('field_name'); 

    function read() 
    { 
     return User::all(); 
    } 
} 

Przykład 2: Ustawianie właściwości visible na klasie wymowny dynamicznie, nie działa:

class User extends Eloquent { 
    function read($visible = array('field_name')) 
    { 
     $this->visible = $visible; // Also tried: $this->setVisible($visible); 

     return User::all(); 
    } 
} 

Przykład 3: Rozwiązanie, które działa na samym modelu, ale nie w modelach z pełnym obciążeniem:

class User extends Eloquent { 
    function read($visible = array('field_name')) 
    { 
     $users = User::all(); 

     return $users->get()->each(function($row) use ($visible) { 
     $row->setVisible($visible); 
     }); 
    } 
} 

Aby ustawić właściwość $ visible dynamicznie w modelach z ładunkiem ładowanym, nie widzę innego rozwiązania, niż uzyskanie przykładu 2. Ale jak?

+0

Mam ten sam problem .. Czy kiedykolwiek byłeś w stanie znaleźć rozwiązanie? – ipengineer

+0

Nie, nadal nie. –

+0

Właśnie obejść to poprzez wykonanie -> select() na metodach wewnątrz mojego modelu. http://stackoverflow.com/questions/16994253/laravel-eager-loading-load-only-specific-columns – ipengineer

Odpowiedz

1

Jako $visible ustawia się na poziomie instancji (tzn. Nie jest statyczną zmienną udostępnianą między wszystkimi modelami tego samego typu), nie - nie ma lepszego sposobu, aby to zrobić.

1

To jest coś wymyślić do tego celu:

use Illuminate\Database\Eloquent\Model; 

    /* 
    * trait sllows to use a single method: 
    *  getSerialized 
    * this function works as the following method: 
    *  Illuminate\Database\Query\Builder::get(), 
    * and also returns the collection 
    * but accepts a parameter of an array type 
    * like that 
    *  $records = Table1::getSerialized([ 
    *    'appends' => ['calc_field1', 'calc_field2'], 
    *    'hidden' => ['field1', 'field2', 'field3'], 
    *    'visible' => ['id', 'name', 'calc_field1', 'calc_field2'], 
    *   ]); 
    * 
    * the returned collection accords with params 
    * read more in Laravel documentation 
    *  https://laravel.com/docs/5.1/eloquent-serialization 
    */ 

    trait Serialization 
    { 
    // scope filters ---------------------------------------------------------   
     private static $staticAppends; 
     private static $staticHidden; 
     private static $staticVisible; 

     public function __construct(array $attributes = []){ 
      parent::__construct($attributes); 
      if (isset(self::$staticAppends)){ 
       $this->appends = self::$staticAppends; 
      } 
      if (isset(self::$staticHidden)){ 
       $this->hidden = self::$staticHidden; 
      } 
      if (isset(self::$staticVisible)){ 
       $this->visible = self::$staticVisible; 
      } 

     } 

     public function scopeGetSerialized($query, array $params){ 

      if (isset(self::$staticAppends)){ 
       $staticAppends = self::$staticAppends; 
      } else { 
       $staticAppends = []; 
      } 
      if (isset(self::$staticHidden)){ 
       $staticHidden = self::$staticHidden; 
      } else { 
       $staticHidden = []; 
      } 
      if (isset(self::$staticVisible)){ 
       $staticVisible = self::$staticVisible; 
      }else { 
       $staticVisible = []; 
      } 

      if (isset($params['appends'])){ 
       self::$staticAppends = $params['appends']; 
      } 
      if (isset($params['hidden'])){ 
       self::$staticHidden = $params['hidden']; 
      } 
      if (isset($params['visible'])){ 
       self::$staticVisible = $params['visible']; 
      } 

      $res = $query->get(); 

      self::$staticAppends = $staticAppends; 
      self::$staticHidden = $staticHidden; 
      self::$staticVisible = $staticVisible; 
      return $res; 
     } 
    } 
Powiązane problemy