How to hide relationship columns in laravel?

前端 未结 4 557
花落未央
花落未央 2021-01-18 01:48

I have a statement like this:

App\\User::with(\'client\')->find(2)->makeHidden(\'client.phone_no\');

I want to hide certain columns f

4条回答
  •  无人及你
    2021-01-18 02:18

    You can create a scope in your model and apply it in builder

    Define these functions in your model

    protected function columns(){
        return Schema::getColumnListing('clients');
    }
    
    public function scopeExclude($query, $value = array()) 
    {
      return $query->select( array_diff( $this->columns(), $value) );
    }
    

    Now use it in query builder

    App\User::with(['client' => function($query){
        $query->exclude(['phone_no']);
    }])->find(2)
    

提交回复
热议问题