问题
How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)
For more complicated queries am I supposed to use raw SQL?
回答1:
Make use of Parameter Grouping (Laravel 4.2). For your example, it'd be something like this:
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
回答2:
If you want to use parameters for a,b,c,d in Laravel 4
Model::where(function ($query) use ($a,$b) {
$query->where('a', '=', $a)
->orWhere('b', '=', $b);
})
->where(function ($query) use ($c,$d) {
$query->where('c', '=', $c)
->orWhere('d', '=', $d);
});
回答3:
if you want to use parentheses in laravel 4 and don't forget return
In Laravel 4 (at least) you need to use $a, $b in parentheses as in the example
$a = 1;
$b = 1;
$c = 1;
$d = 1;
Model::where(function ($query) use ($a, $b) {
return $query->where('a', '=', $a)
->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
return $query->where('c', '=', $c)
->orWhere('d', '=', $d);
});
This is my result:
回答4:
Incase you're looping the OR conditions, you don't need the the second $query->where from the other posts (actually I don't think you need in general, you can just use orWhere in the nested where if easier)
$attributes = ['first'=>'a','second'=>'b'];
$query->where(function ($query) use ($attributes)
{
foreach ($attributes as $key=>value)
{
//you can use orWhere the first time, doesn't need to be ->where
$query->orWhere($key,$value);
}
});
回答5:
$a, $b, $c, $d can be dynamic values by the query
->where(function($query) use ($a, $b)
{
$query->where('a', $a)
->orWhere('b',$b);
})
->where(function($query) use ($c, $d)
{
$query->where('c', $c)
->orWhere('d',$d);
})
回答6:
Simply Use in Laravel Eloquent:
$a='foo', $b='bar', $c='john', $d='doe';
Coder::where(function ($query) use ($a, $b) {
$query->where('a', '=', $a)
->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
$query->where('c', '=', $c)
->orWhere('d', '=', $d);
})->get();
Will produce a query like:
SELECT * FROM <table> WHERE (a='foo' or b='bar') AND (c='john' or d='doe');
回答7:
You can also use query scopes to make things a bit tidier, so you can do something like:
Invoice::where('account', 27)->notPaidAt($date)->get();
Then in your model
public function scopeNotPaidAt($query, $asAt)
{
$query = $query->where(function ($query) use ($asAt) {
$query->where('paid', '=', '0000-00-00')->orWhere('paid', '>=', $asAt);
});
return $query;
}
回答8:
You can also query the first or condition and later you can apply another or condition
$model = Model::where('a',1)->orWhere('b',1);
now apply another condition on that $model
variable
$model1 = $model->where('c',1)->orWhere('d',1)->get();
回答9:
Another way without using Modal
Database: stocks Columns:id,name,company_name,exchange_name,status
$name ='aa'
$stocks = DB::table('stocks')
->select('name', 'company_name', 'exchange_name')
->where(function($query) use ($name) {
$query->where('name', 'like', '%' . $name . '%')
->orWhere('company_name', 'like', '%' . $name . '%');
})
->Where('status', '=', 1)
->limit(20)
->get();
回答10:
YourModel::where(function ($query) use($a,$b) {
$query->where('a','=',$a)
->orWhere('b','=', $b);
})->where(function ($query) use ($c,$d) {
$query->where('c','=',$c)
->orWhere('d','=',$d);
});
回答11:
Best way to use sql brackets use callback function in laravel eloquent.
YourModal::where(function ($q) {
$q->where('a', 1)->orWhere('b', 1);
})->where(function ($q) {
$q->where('c', 1)->orWhere('d', 1);
});
You don't have to use
=
symbol, it's come as the default
Lest say if you have a query that contain brackets inside a brackets
WHERE (a = 1 OR (b = 1 and c = 5))
YourModal::where(function ($q) {
$q->where('a', 1)->orWhere(function($q2){
$q2->where('b', 1)->where('c', 5);
});
});
lest say you want to make values dynamics
YourModal::where(function ($q) use($val1, $val2) {
$q->where('a', $val1)->orWhere(function($q2) use($val2){
$q2->where('b', $val2)->where('c', $val2);
});
});
来源:https://stackoverflow.com/questions/16995102/laravel-4-eloquent-query-using-where-with-or-and-or