Eloquent 的 Model::query() 是什么意思?

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

谁能详细解释一下Eloquent的Model::query()是什么意思?

任何时候在 Eloquent 中查询模型,您都在使用 Eloquent 查询生成器。 Eloquent 模型使用魔术方法(__call、__call静态)将调用传递给查询构建器。 Model::query() returns 此查询生成器的一个实例。

因此,由于 where() 和其他查询调用被传递给查询构建器:

Model::where()->get();

等同于:

Model::query()->where()->get();

过去我发现自己使用 Model::query() 的地方是当我需要实例化一个查询,然后根据请求变量建立条件时。

$query = Model::query();
if ($request->color) {
    $query->where('color', $request->color);
}

希望这个例子对您有所帮助。