2013-06-04 21 views
8

Khi sử dụng ORX của Eloquent của Laravel, dường như tôi không thể đặt thuộc tính $ hidden và $ visible trên Mô hình của mình theo kiểu động.Laravel/Eloquent - Các thuộc tính ẩn/hiển thị được tải xuống

Ví dụ 1: Đây hoạt động:

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

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

Ví dụ 2: Thiết lập thuộc tính có thể nhìn thấy trên lớp hùng biện tự động, không hoạt động:

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

     return User::all(); 
    } 
} 

Ví dụ 3: Giải pháp hoạt động trên Bản thân mô hình, nhưng không hoạt động trên các Mô hình được háo hức nạp:

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

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

Để đặt thuộc tính $ có thể nhìn thấy động trên Mô hình được tải trọng, tôi không thấy giải pháp nào khác ngoài việc làm cho Ví dụ 2 hoạt động. Nhưng bằng cách nào?

+0

Tôi gặp cùng một vấn đề .. Were bạn đã bao giờ có thể tìm thấy một giải pháp? – ipengineer

+0

Không, vẫn không. –

+0

Tôi chỉ làm việc này bằng cách thực hiện một -> select() trên các phương thức bên trong mô hình của tôi. http://stackoverflow.com/questions/16994253/laravel-eager-loading-load-only-specific-columns – ipengineer

Trả lời

1

$visible được đặt ở cấp độ thể hiện (nghĩa là không phải biến tĩnh được chia sẻ giữa tất cả các kiểu cùng loại), không - không có cách nào tốt hơn để thực hiện việc này.

1

Đây là điều mà tôi phát minh ra cho mục đích này:

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; 
     } 
    } 
Các vấn đề liên quan