Reputation: 644
I have three tables which is connected like.
packages(parent) -> routes(child) -> items(grandchild)
i have to find all items with package id using laravel eloquent i know this can be done by using join but what we do if use eloquent
+---------+ +-----------------------+ +---------------------+
|packages | | routes | | items |
+---------+ +-----------------------+ +---------------------+
|id | name| |id | package_id |name | |id | route_id |name |
| 1 | P1 | | 1 | 1 | R1 | | 1 | 1 | I1 |
| 2 | P2 | -> | 2 | 1 | R2 | -> | 2 | 1 | I2 |
| 3 | P3 | | 3 | 2 | R3 | | 3 | 2 | I3 |
| 4 | P4 | | 4 | 2 | R4 | | 4 | 1 | I4 |
| 5 | P5 | | 5 | 3 | R5 | | 5 | 3 | I5 |
+---------+ +-----------------------+ +---------------------+
Pacakge Model
here where i have to achive this
class Package extends Model
{
public function items(){
return $this->...;
}
}
result (item which is belongs to package_id = 1)
+-----------------------+
|id | route_id | name |
+-----------------------+
| 1 | 1 | I1 |
| 2 | 1 | I2 |
| 3 | 2 | I3 |
| 4 | 1 | I4 |
+-----------------------+
is there any way to do that using eloquent
thanks in advance
Upvotes: 1
Views: 316
Reputation: 3679
you need to do this:
class Package extends Model
{
public function items()
{
return $this->hasManyThrough('App\Item', 'App\Route',
'package_id', 'route_id', 'id');
}
}
Upvotes: 2