How to hide relationship columns in laravel?

前端 未结 4 560
花落未央
花落未央 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 01:59

    With accepts a callback to modify the query.

    $users = User::with(['client' => function($query) {
            $query->select(['id', 'name']);
        }])->find(2);
    

    You could also define default hidden attributes on your Client model

    protected $hidden = ['phone_no'];
    

提交回复
热议问题