You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While the global `query(Model::class)` function creates a query builder for any model, models using the `IsDatabaseModel` trait also have a `query()` method that returns a query builder scoped to a specific relation. The returned `QueryBuilder` is pre-filtered to only include records belonging to that model:
The `query()` method works with all relation types:
780
+
781
+
```php
782
+
// HasMany / HasOne — simple FK on related table
783
+
$author->query('books')->select()->all();
784
+
$book->query('isbn')->select()->first();
785
+
786
+
// BelongsTo — subquery through owner's FK
787
+
$book->query('author')->select()->first();
788
+
789
+
// HasManyThrough / HasOneThrough — subquery through intermediate table
790
+
$tag->query('reviewers')->select()->all();
791
+
$tag->query('topReviewer')->select()->first();
792
+
793
+
// BelongsToMany — subquery through pivot table
794
+
$tag->query('books')->select()->all();
795
+
```
796
+
760
797
## Migrations
761
798
762
799
When persisting objects to the database, a table is required to store the data. A migration is a file that instructs the framework how to manage the database schema.
0 commit comments