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();
1When updating a belongsTo relationship, you may use the associate method. This
2method will set the foreign key on the child model:
3
4 $account = App\Account::find(10);
5 $user->account()->associate($account);
6 $user->save();
7
8When removing a belongsTo relationship, you may use the dissociate method. This
9method will set the relationship foreign key to null:
10
11 $user->account()->dissociate();
12 $user->save();