Does anyone have any suggestions for how to use makeHidden()
when eager-loading? here is my code:
$work=Work::with([
'work_category'=>function($query){
$query->with(['companies'=>function($query){
$query->select('companies.id','companies.name');
}]);
},
'prices',
])
->findOrFail($id);
Work has a belongsTo('App\WorkCategory')
relationship, and WorkCategory has a belongsToMany('App\Company')
relationship through a pivot.
If I try to chain ->makeHidden('pivot')
on to $query->select('companies.id','companies.name');
- I get a BadMethodCall exception: Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::makeHidden()
Is there something I'm missing here?
This is the code with the offending makeHidden() call
$work=Work::with([
'work_category'=>function($query){
$query->with(['companies'=>function($query){
$query->select('companies.id','companies.name')->makeHidden('pivot');
}]);
},
'prices',
])
->findOrFail($id);
My temporary fix has been to add protected $hidden=['pivot'];
to my Company model, however it would be nice to be able to access the pivot when I do need it, and use $model->relation->makeHidden('attribute')
in my controllers to trim away extra data before sending.