Vis*_*nav 4 php laravel laravel-5
以下是我的查询,
rows = "SELECT * FROM `table` WHERE score = 1"
if (rows.Count < 3) //at least one row
return if;
else if(rows.Count >7)
return 'else if';
else
return 'else';
Run Code Online (Sandbox Code Playgroud)
使用 querybuilder laravel 时如何编写上述查询。其实我想知道else条件怎么写。
下面是我的代码;
$query=DB::table('aaa')
->select('*')
->when($count<3,function ($q){
echo 'if';
})
->when($count>7,function ($q){
echo 'else if';
})
///I dont know how to write else condition here
Run Code Online (Sandbox Code Playgroud)
Qir*_*rel 11
当使用多个条件时( )没有else对应的情况,但如果你仔细想想,它只是其他条件之和的倒数。要精确镜像 PHP ,您需要检查第一个条件,然后显式检查第二个条件而不是第一个条件,并且为了模仿该条件,它只是当所有其他条件失败时需要的布尔条件。when()if/elseif/elseif/elseif/elseelse
$query = DB::table('table')
->select('*')
->when($count < 3, function ($q) {
// if count < 3
})
->when($count > 7, function ($q) {
// else if count > 7
})
->when($count =< 7 && $count >= 3, function($q) {
// Else, count between 3 and 7 (inclusive)
});
Run Code Online (Sandbox Code Playgroud)
您可以使用本机 PHPif/elseif/else控件有条件地“手动”应用 where 子句并应用一个简单的where(),如果您的条件变得非常具有表现力或复杂,这可能会更加明确。
else然而,当你有一个 simple 时,有一个条件if/else,因为你可以为它提供另一个闭包。正如您最初询问的那样,这不能与多个条件一起使用,但我将其包含在内以供参考。
$query = DB::table('table')
->select('*')
->when($count < 3, function ($q) {
// if count < 3
}, function($q) {
// Else, count greater or equal to 3
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13753 次 |
| 最近记录: |