From 676c6286aefcca66a80df8726b9bd66c9d04f712 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 6 Oct 2021 01:31:36 +0500 Subject: [PATCH] added whereBelongsTo methods in where section of query builder docs --- queries.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/queries.md b/queries.md index 0203b49f44..3a78fac193 100644 --- a/queries.md +++ b/queries.md @@ -595,6 +595,26 @@ You may also pass an array of column comparisons to the `whereColumn` method. Th ['updated_at', '>', 'created_at'], ])->get(); +**whereBelongsTo** + +When retrieving `belongsTo` related records, you can usually use the inverse (hasMany) of the relationship, like so: + + $author->posts() + +However, sometimes this is not possible, and you must filter an existing query by its parent record: + + $query->where('author_id', $author->id) + +This works, but explicitly depends on the name of foreign key (`author_id`), and the name of the owner key (`id`) in most cases. This is a maintenance burden, as these key names can be changed inside the relationship method and will subsequently become outdated across your entire app. So query builder has `whereBelongsTo`. You may use it like so: + + $query->whereBelongsTo($author) + +It will automatically retrieve the foreign key name from the relationship (`author`), and the correct owner key from the related model (`$author`). + +Sometimes, this is not appropriate. If the `$author` is an instance of `App\Models\User`, `whereBelongsTo()` will search for a `user()` relationship that does not exist. In this case, you may manually specify the relationship name: + + $query->whereBelongsTo($author, 'author') + ### Logical Grouping