Hide rotation attribute

I know that you can hide the whole pivot table as such

protected $hidden = ['pivot']; 

How do you hide a specific field in a pivot table like

 protected $hidden = ['pivot.created_at']; 

The above does not work with what I tested

+5
source share
1 answer

After so many attempts and searching the source of the Laravel Model , I finally got it.

Please put the following method in your model.

 /** * Convert the model instance to an array. * * @return array */ public function toArray() { $attributes = $this->attributesToArray(); $attributes = array_merge($attributes, $this->relationsToArray()); unset($attributes['pivot']['created_at']); return $attributes; } 

This solves the goal.

+3
source

All Articles