php - Eloquent 中的 If request is not null 语句

标签 php laravel

如何仅当请求存在时才执行查询中的语句? 示例:$request->type(如果为 NULL,则到处搜索,如果不检查,则仅检查到达的文件)

$flights = App\Flight::where('active', 1)
               ->orderBy('name', 'desc')
               ->if($requst->type = NULL) return all; else { ->where('type', $request->type)
               ->get();

谢谢你的帮助! :)

最佳答案

您可以拆分代码重新分配查询,例如

$flights = App\Flight::where('active', 1)
               ->orderBy('name', 'desc');
if($requst->type != NULL) {
    $flights = $flights->where('type', $request->type);
}
return $flights->get();

关于php - Eloquent 中的 If request is not null 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970648/

相关文章:

php - 显示 MySQL 最后 5 条不同的搜索结果

php - Laravel 模型/ View 关系

php - Laravel 使用 belongstoMany 从 ID 获取模型

php - 无法检索 PDOStatement 受影响的行数

php - 未捕获的 PDOException : could not find driver inside docker container

javascript - 在特定秒数后运行 javascript

php - 无法通过 PHPmailer 连接到 Office365 SMTP

php - 我应该在哪个 XML 文件中使用 depends 元素 (Magento)?

jquery - 为 Laravel Collective Form 创建手动提交按钮

php - 如何将div背景颜色突出显示为红色?