Instance the query builder directly from model

前端 未结 1 1921
天涯浪人
天涯浪人 2021-02-06 23:12

When I do something like SomeModel::with(\'user\') it returns a Query\\Builder instance. How can I get this instance without need call the with()

相关标签:
1条回答
  • 2021-02-06 23:45

    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.

    0 讨论(0)
提交回复
热议问题