1<?php
2
3namespace App;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Category extends Model
8{
9 public function orders()
10 {
11 return $this->hasManyThrough(
12 'App\Order',
13 'App\Product',
14 'category_id', // Foreign key on products table...
15 'product_id', // Foreign key on orders table...
16 'id', // Local key on categories table...
17 'id' // Local key on products table...
18 );
19 }
20}
21
22// full answer is in source: https://xpertphp.com/laravel-hasmanythrough-eloquent-relationship-tutorial-example/
1<?php
2
3namespace App;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Mechanic extends Model
8{
9 /**
10 * Get the car's owner.
11 */
12 public function carOwner()
13 {
14 return $this->hasOneThrough('App\Owner', 'App\Car');
15 }
16}
1class Country extends Model
2{
3 public function posts()
4 {
5 return $this->hasManyThrough(
6 'App\Post',
7 'App\User',
8 'country_id', // Foreign key on users table...
9 'user_id', // Foreign key on posts table...
10 'id', // Local key on countries table...
11 'id' // Local key on users table...
12 );
13 }
14}
15
16when
17countries
18 id - integer
19 name - string
20
21users
22 id - integer
23 country_id - integer
24 name - string
25
26posts
27 id - integer
28 user_id - integer
29 title - string