laravel - where 语句内有多个 where

标签 laravel

我想在 Laravel 中生成一个查询,给出以下结果:

从获得的结果

$results = DB::table('records')->where('email', '!=', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d19081e192d0a000c0401430e0200" rel="noreferrer noopener nofollow">[email protected]</a>')->get();

执行多个 where 语句,如下所示:

->where('id', 'like', '%'.$request->search.'%')
->orWhere('email', 'like', '%'.$request->search.'%')
->orWhere('recordType', 'like', '%'.$request->search.'%')
->orWhere('uploadDate', 'like', '%'.$request->search.'%')
->orWhere('uploadTime', 'like', '%'.$request->search.'%')
->get();

我应该如何在 Controller 中表达该语句以产生所需的结果?

最佳答案

使用where() closure对参数进行分组:

$results = DB::table('records')->where('email', '!=', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5420312720143339353d387a373b39" rel="noreferrer noopener nofollow">[email protected]</a>')
    ->where(function($q) use($request) {
        $q->where('id', 'like', '%' . $request->search . '%')
          ->orWhere('email', 'like', '%' . $request->search . '%')
          ->orWhere('recordType', 'like', '%' . $request->search . '%')
          ->orWhere('uploadDate', 'like', '%' . $request->search . '%')
          ->orWhere('uploadTime', 'like', '%' . $request->search . '%');
    })
    ->get();

关于laravel - where 语句内有多个 where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429586/

相关文章:

php - 使用从表单收到的输入更新我的产品表中的价格列

php - 在 laravel 中对齐 Bootstrap 网格

php - 找不到类 'GuzzleHttp\HandlerStack'

php - 如何在链接到一个主键的数据透视表上创建两个外键

php - Laravel 4 查询生成器(连接)

php - 根据路由组在错误页面中加载页脚

php - 如何在 Laravel Eloquent 中确定结果的优先级

php - 不同开发环境下的 Laravel

php - ErrorException + 取消链接 + 资源暂时不可用 + Laravel 5.5

netbeans - Netbeans 的 Laravel 自动格式化