What is the meaning of Eloquent’s Model::query()?

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);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)