where语句中的多个where [英] Multiple where inside a where statement

查看:128
本文介绍了where语句中的多个where的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to generate a query in Laravel that gives the following results:

从获得的结果

$results = DB::table('records')->where('email', '!=', 'test@gmail.com')->get();

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

Do multiple where statements like this:

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

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

How should I phrase the statement in controller to produce the desired result?

推荐答案

使用 where()闭合将参数分组:

Use the where() closure to group parameters:

$results = DB::table('records')->where('email', '!=', 'test@gmail.com')
    ->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();

这篇关于where语句中的多个where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆