I have two models, User
and Badge
. A user can have multiple badges, and a badge can belong to multiple users. (using a pivot table)
Currently I am getting the data I need, but additionally I am getting the pivot
table along. How do I exclude this?
Here's the User
model:
class User extends Eloquent { public function badges() { return $this->belongsToMany('Badge', 'users_badges'); } }
And the Badge
model:
class Badge extends Eloquent { public function users() { return $this->belongsToMany('User', 'users_badges'); } }
Add pivot
to your $hidden
property's array in your model(s).
class Badge extends Eloquent { protected $hidden = ['pivot']; public function users() { return $this->belongsToMany('User', 'users_badges'); } }
And same with your User
model
class User extends Eloquent { protected $hidden = ['pivot']; public function badges() { return $this->belongsToMany('Badge', 'users_badges'); } }
Or you can still hide the pivot on demand this way...
$user = User::find(1); $user->badges->makeHidden('pivot'); $badge = Badge::find(1); $badge->users->makeHidden('pivot');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With