1
votes

How to combine 3 Many to Many tables with eloquent in the laravel Hi Friends, please help me,

How do you combine the following three tables with eloquent in laravel?

The following table structure and figures: (https://i.stack.imgur.com/BfISA.jpg)

Member table structure {member_id: primary key, name, created_at, updated_at}

List table structure {list_id: primary key, member_id: foregn key, price_id: foreign key, created_at, updated_at}

Price table structure {price_id: primary key, name_price, created_at, updated_at}

Can you give me sample source code for view.blade, controller and model.

Thank you, your answer is very meaningful

1
Are you trying to join all this three tables?Rehan

1 Answers

0
votes

in your member model add relationship function list(). Then in your list model add relationship function price then use below code

app\Member.php

public function list(){
        return $this->hasMany('App\List', 'member_id');
}

app\List.php

public function price(){
        return $this->hasMany('App\Price', 'price_id');
}

in your controller

$result = Member::with(['list.price'])->get();

it will give you a member list with list and price table data.