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();
1Event::with(["owner", "participants" => function($q) use($someId){
2 $q->where('participants.IdUser', '=', 1);
3 //$q->where('some other field', $someId);
4}])
1<?php
2
3namespace App;
4
5use Illuminate\Database\Eloquent\Model;
6
7class User extends Model
8{
9 /**
10 * Get the phone record associated with the user.
11 */
12 public function phone()
13 {
14 return $this->hasOne('App\Phone');
15 }
16}
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<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Phone extends Model
8{
9 /**
10 * Get the user that owns the phone.
11 */
12 public function user()
13 {
14 return $this->belongsTo('App\Models\User');
15 }
16}
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Post extends Model
8{
9 /**
10 * Get the comments for the blog post.
11 */
12 public function comments()
13 {
14 return $this->hasMany('App\Models\Comment');
15 }
16}