在Laravel中组合AND / OR eloquent查询

时间:2022-09-18 20:21:09

How can I write following or similar kind of queries using Eloquent?

如何使用Eloquent编写以下或类似的查询?

SELECT * FROM a_table WHERE (a LIKE %keyword% OR b LIKE %keyword%) AND c = 1 AND d = 5

SELECT * FROM a_table WHERE(LIKE%keyword%OR b LIKE%keyword%)AND c = 1 AND d = 5

I couldn't combine AND/OR in the way I wanted by chaining where & or_where functions.

我无法通过链接where&or_where函数以我想要的方式组合AND / OR。

4 个解决方案

#1


34  

You can nest where clauses : http://laravel.com/docs/database/fluent#nested-where

您可以在where子句中嵌套:http://laravel.com/docs/database/fluent#nested-where

Model::where(function($query)
{
    $query->where('a', 'like', 'keyword');
    $query->or_where('b', 'like', 'keyword');
})
->where('c', '=', '1');

This should produce : SELECT * FROM models WHERE (a LIKE %keyword% OR b LIKE %keyword%) AND c = 1

这应该产生:SELECT * FROM models WHERE(LIKE%keyword%OR b LIKE%keyword%)AND c = 1

#2


7  

For a more accurate answer to the example:

要获得更准确的示例答案:

$val = '%keyword%';

A_Table_Model::where(function($query) use ($val)
{
    $query->where('a', 'like', $val);
    $query->or_where('b', 'like', $val);
})
->where('c', '=', 1)
->where('d', '=', 5)
->get();

Note: This is Laravel 3 syntax, use camelCase orWhere() for Laravel 4

注意:这是Laravel 3语法,对Laravel 4使用camelCase或Where()

#3


2  

In Laravel 5.1+ this will also do the job and looks cleaner:

在Laravel 5.1+中,这也可以完成工作,看起来更干净:

Model::where(function($query) {
    $query->where('a', 'like', 'keyword');
    $query->or_where('b', 'like', 'keyword');
})->where('c', '=', '1')->get();

#4


-3  

You can use DB::raw() in the first where() to put in the like/or statement.

您可以在第一个where()中使用DB :: raw()来放入like /或statement。

#1


34  

You can nest where clauses : http://laravel.com/docs/database/fluent#nested-where

您可以在where子句中嵌套:http://laravel.com/docs/database/fluent#nested-where

Model::where(function($query)
{
    $query->where('a', 'like', 'keyword');
    $query->or_where('b', 'like', 'keyword');
})
->where('c', '=', '1');

This should produce : SELECT * FROM models WHERE (a LIKE %keyword% OR b LIKE %keyword%) AND c = 1

这应该产生:SELECT * FROM models WHERE(LIKE%keyword%OR b LIKE%keyword%)AND c = 1

#2


7  

For a more accurate answer to the example:

要获得更准确的示例答案:

$val = '%keyword%';

A_Table_Model::where(function($query) use ($val)
{
    $query->where('a', 'like', $val);
    $query->or_where('b', 'like', $val);
})
->where('c', '=', 1)
->where('d', '=', 5)
->get();

Note: This is Laravel 3 syntax, use camelCase orWhere() for Laravel 4

注意:这是Laravel 3语法,对Laravel 4使用camelCase或Where()

#3


2  

In Laravel 5.1+ this will also do the job and looks cleaner:

在Laravel 5.1+中,这也可以完成工作,看起来更干净:

Model::where(function($query) {
    $query->where('a', 'like', 'keyword');
    $query->or_where('b', 'like', 'keyword');
})->where('c', '=', '1')->get();

#4


-3  

You can use DB::raw() in the first where() to put in the like/or statement.

您可以在第一个where()中使用DB :: raw()来放入like /或statement。