php Laravel/Eloquent -急切加载隐藏/可见属性

ztyzrc3y  于 2023-01-12  发布在  PHP
关注(0)|答案(3)|浏览(166)

当使用Laravel的Eloquent ORM时,我似乎不能动态地设置模型的$hidden和$visible属性。

示例1:这是可行的:

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

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

示例2:动态设置Eloquent类的visible属性无效:

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

      return User::all();
   }
}

示例3:解决方案适用于模型本身,但不适用于急切加载的模型:

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

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

为了在Eagerly Loaded Models上动态地设置$visible属性,除了让Example 2工作之外,我没有看到其他的解决方案。

esbemjvw

esbemjvw1#

由于$visible是在示例级别上设置的(也就是说,它不是一个在所有相同类型的模型之间共享的静态变量),不--没有更好的方法来做到这一点。

baubqpgj

baubqpgj2#

这是我为此发明的东西:

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

vfh0ocws3#

如果您打算只使用这些属性,可以尝试直接从查询中只使用select这些属性。

User::select(['id', 'field_name_1'])->get();

相关问题