有没有人能详细解释一下口才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();

与以下内容相同:

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

Hope this example helps.

Laravel相关问答推荐

未将Laravel Blade属性传递给组件或类

如何解决此 Backblaze B2 S3 兼容 API 错误?

我如何通过 laravel 在高图中针对不同的时间范围进行烛台数据分组

assertSee 由于 html 代码中的空格而失败

Laravel + Plupload 上传到 S3 响应以进行预检无效 - CORS

集成测试模拟外观与注入模拟

自定义 Laravel 关系?

如何使用 Laravel 模型访问数据库视图?

Laravel - 如何恢复本地存储符号链接并刷新

使用 Laravel Socialite 登录 Facebook

用 laravel 表单确认删除?

SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4

Laravel 5 文件下载:stream() 或 download()

Twilio 查找 API 不起作用?

如何在 laravel blade中进行一次性推送

Laravel:方法[显示]不存在

Laravel 在保存前生成 slug

Laravel 应用程序连接数据库时速度很慢

Laravel 如何从子域 URL 中删除api前缀

Lumen/Laravel 6:调用未定义的函数 array_except()