Can anyone please explain in detail what Eloquent's Model::query()
means?
Asked
Active
Viewed 2.7k times
30

Shateel Ahmed
- 1,264
- 2
- 12
- 23
-
4it returns the query builder for the given model ! – Maraboc Jul 25 '18 at 10:57
1 Answers
72
Any time you're querying a Model in Eloquent, you're using the Eloquent Query Builder. Eloquent models pass calls to the query builder using magic methods (__call, __callStatic). Model::query()
returns an instance of this query builder.
Therefore, since where()
and other query calls are passed to the query builder:
Model::where()->get();
Is the same as:
Model::query()->where()->get();
Where I've found myself using Model::query() in the past is when I need to instantiate a query and then build up conditions based on request variables.
$query = Model::query();
if ($request->color) {
$query->where('color', $request->color);
}

ruleboy21
- 5,510
- 4
- 17
- 34

Devon Bessemer
- 34,461
- 9
- 69
- 95
-
2Thanks @Devon. Can you provide a reference of the documentation? – Shateel Ahmed Jul 26 '18 at 05:08
-
What documentation are you looking for exactly? The query builder's? You can look at the API documentation or the Model class itself to see what it returns. – Devon Bessemer Jul 26 '18 at 13:04
-
-
1@Shulz I would recommend using the relation methods built into Eloquent than doing your own join with the eloquent model. Otherwise, you can use the Query Builder as standalone and write a query that returns loosely typed objects – Devon Bessemer Sep 20 '21 at 14:36
-
-
1Why there is no documentation for query() method in the Laravel documentation? – CuriousTeam Dec 27 '22 at 19:24