CherryBelle
CherryBelle

Reputation: 1422

Laravel Eloquent Join Multiple Table

Table A
- id
- name

Table B
- id_a
- id_c

Table C
- id
- value

I have three tables. In the eloquent model of Table A, I need to join it with Table B, then with Table C so I can get the value. The relationship between A and B is one to many. Sometimes, the table B doesn't contain id_a of a record in table A. So I need left outer join. Is it possible to use Eloquent or if it's not possible, how to make the DB::table('A') query?

Upvotes: 0

Views: 1236

Answers (1)

Lance Pioch
Lance Pioch

Reputation: 1167

This might be a good case to use the HasManyThrough relationship type for Laravel: https://laravel.com/docs/5.2/eloquent-relationships#has-many-through

In your A.php model (assuming you have a B and a C model as well):

public function C()
{
    return $this->hasManyThrough('App\C', 'App\B');
}

Upvotes: 0

Related Questions