Instance the query builder directly from model

Use the static query method:

$query = User::query();

Additionally, you can use the when method to chain these conditionals directly onto the query builder itself:

$results = SomeModel::query()->when(condition(), function ($query) {
    $query->where(...);
})->get();

This is functionally equivalent to the imperative if clause.

Leave a Comment