1use Illuminate\Database\Eloquent\Builder;
2
3// Retrieve posts with at least one comment containing words like foo%...
4$posts = App\Post::whereHas('comments', function (Builder $query) {
5 $query->where('content', 'like', 'foo%');
6})->get();
7
8// Retrieve posts with at least ten comments containing words like foo%...
9$posts = App\Post::whereHas('comments', function (Builder $query) {
10 $query->where('content', 'like', 'foo%');
11}, '>=', 10)->get();
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
1use Illuminate\Database\Eloquent\Builder;
2
3// Retrieve posts with at least one comment containing words like code%...
4$posts = Post::whereHas('comments', function (Builder $query) {
5 $query->where('content', 'like', 'code%');
6})->get();
7
8// Retrieve posts with at least ten comments containing words like code%...
9$posts = Post::whereHas('comments', function (Builder $query) {
10 $query->where('content', 'like', 'code%');
11}, '>=', 10)->get();
1$movies = Movie::whereHas('director', function($q) {
2 $q->where('name', 'great');
3})->get();
4