我使用的是laravel 8。我有一个包含四列的数据透视表:
- user_id
- 操作员id
- 折扣
- local_discount
我定义了两个关系,一个用于我的项目,另一个用于API
public function operators(){
return $this->belongsToMany(Operator::class,'reseller_rates','user_id','operator_id')->withPivot(['discount','local_discount']);
}
public function api_operators(){
return $this->belongsToMany(Operator::class,'reseller_rates','user_id','operator_id')->as('rates')->withPivot(['discount','local_discount']);
}
但当我使用这种关系时,它给出了数据透视表的所有四列
$operators = $user['api_operators']
我知道我可以使用laravel的makeHidden方法隐藏运算符表的一些列
$operators->makeHidden(['discount','local_discount']);
但是,我如何隐藏user_id和operator_id,它们是随透视数据返回的。有没有办法将makeHidden与透视表的嵌套属性一起使用来隐藏user_id和operator_id?或者任何其他更好的方式来实现这一点。
感谢
如果你想从你的api中隐藏这些列,我会选择api资源:https://laravel.com/docs/8.x/eloquent-resources#introduction