Laravel 4 wymowne zapytanie używając WHERE z OR I OR?
How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)
Dla bardziej skomplikowanych zapytań czy powinienem używać surowego SQL?
11 answers
Użyj grupowanie parametrów (Laravel 4.2). Dla Twojego przykładu byłoby to coś takiego:
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-20 16:49:52
Jeśli chcesz użyć parametrów dla a,b,c, D w 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);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-13 05:23:02
Jeśli chcesz użyć nawiasów w laravel 4 i nie zapomnij return
W Laravel 4 (przynajmniej) musisz użyć $a, $b w nawiasach jak w przykładzie
$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);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-13 06:34:53
Jeśli zapętlasz warunki OR, nie potrzebujesz drugiego $query - > where z innych postów (właściwie nie sądzę, że potrzebujesz w ogóle, możesz po prostu użyć orWhere w zagnieżdżonym gdzie, jeśli łatwiej)
$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);
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-06 14:43:41
Po prostu Użyj w 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();
Wyświetli zapytanie typu:
SELECT * FROM <table> WHERE (a='foo' or b='bar') AND (c='john' or d='doe');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-23 19:40:58
$a, $B, $c, $ d mogą być wartościami dynamicznymi za pomocą zapytania
->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);
})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-10 05:51:59
Inny sposób bez użycia modalu
Baza Danych: zapasy Kolumny: 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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-21 18:39:03
Możesz również odpytywać pierwszy warunek or, a później możesz zastosować inny warunek or
$model = Model::where('a',1)->orWhere('b',1);
Teraz Zastosuj inny warunek na tej zmiennej $model
$model1 = $model->where('c',1)->orWhere('d',1)->get();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-22 10:51:39
Możesz również użyć zakresów zapytań, aby uczynić rzeczy bardziej uporządkowanymi, więc możesz zrobić coś takiego:
Invoice::where('account', 27)->notPaidAt($date)->get();
Następnie w modelu
public function scopeNotPaidAt($query, $asAt)
{
$query = $query->where(function ($query) use ($asAt) {
$query->where('paid', '=', '0000-00-00')->orWhere('paid', '>=', $asAt);
});
return $query;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-16 14:50:52
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);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-07 13:26:32
Najlepszy sposób użycia nawiasów sql użyj funkcji callback w Laravel eloquent.
YourModal::where(function ($q) {
$q->where('a', 1)->orWhere('b', 1);
})->where(function ($q) {
$q->where('c', 1)->orWhere('d', 1);
});
Nie musisz używać symbolu
=
, jest on domyślny
Jeśli masz zapytanie zawierające nawiasy w nawiasach
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);
});
});
By nie powiedzieć, że chcesz dynamikę wartości
YourModal::where(function ($q) use($val1, $val2) {
$q->where('a', $val1)->orWhere(function($q2) use($val2){
$q2->where('b', $val2)->where('c', $val2);
});
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-06-17 11:46:53