Laravel/Eloquent - Eager loaded hidden/visible properties

前端 未结 2 651
广开言路
广开言路 2021-01-18 00:56

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:

相关标签:
2条回答
  • 2021-01-18 01:33

    This is something I invent for this purpose:

      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;
          }
      }
    
    0 讨论(0)
  • 2021-01-18 01:35

    As $visible is set on an instance level (i.e. it's not a static variable shared between all models of the same type), no - there's no better way to do this.

    0 讨论(0)
提交回复
热议问题