8

When using Laravel's Eloquent ORM, I can't seem to set the $hidden and $visible properties on my Model dynamically.

Example 1: This works:

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

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

Example 2: Setting the visible property on the Eloquent Class dynamically, doesn't work:

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

      return User::all();
   }
}

Example 3: Solution that works on the Model itself, but not on eagerly loaded Models:

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

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

In order to set the $visible property dynamically on Eagerly Loaded Models, I don't see another solution than to get Example 2 to work. But how?

4

2 回答 2

2

正如$visible在实例级别上设置的那样(即它不是同一类型的所有模型之间共享的静态变量),不 - 没有更好的方法来做到这一点。

于 2014-01-03T11:52:35.447 回答
2

这是我为此目的而发明的:

  use Illuminate\Database\Eloquent\Model;

  /*
   * trait allows 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;
      }
  }
于 2016-07-11T01:09:05.927 回答