Mortadda Jafar Mortadda Jafar - 1 year ago 135
PHP Question

Laravel 5.2 relationship with pivot table

my tables:


  1. every part contain many of card.

  2. every card belong to many of part.

now,using laravel eloquent model how can fetch all card for a part without add more column to database

Answer Source

You need to define your relationships like below:

class Part extends Model
    public function cards()
        return $this->belongsToMany('App\Cards', 'user_cards');

Then you can fetch all the cards for a part like below:

$cards = Part::with('cards')->find($part_id);