Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel multiple where子句通过变量检查空值_Php_Laravel_Eloquent - Fatal编程技术网

Php Laravel multiple where子句通过变量检查空值

Php Laravel multiple where子句通过变量检查空值,php,laravel,eloquent,Php,Laravel,Eloquent,我在变量中存储where子句 if($condition === 1) { $whereClause = ['mID' => NULL, 'qid' => $listing->lID, 'deleted' => false]; } else { $whereClause= ['mID' => $member->mID, 'qid' => $listing->lID, 'deleted' => false]; } 我通过 $c

我在变量中存储where子句

if($condition === 1) {
    $whereClause = ['mID' => NULL, 'qid' => $listing->lID, 'deleted' => false];
}
else {
    $whereClause= ['mID' => $member->mID, 'qid' => $listing->lID, 'deleted' => false];
}
我通过

$collection = Model::where($whereClause)
但在第一个条件下,我实际上希望mID为0或NULL,我希望检索mID可以为0或NULL的项,因此我需要orWhere子句,我使用了orWhere子句,如下所示,但这不起作用,它给出了内存耗尽的错误,因此出现了一些问题

if($condition === 1) {
    $whereClause = ['mID' => NULL, 'qid' => $listing->lID, 'deleted' => false];
    $orWhereClause = ['mID' => 0, 'qid' => $listing->lID, 'deleted' => false];

     $collection = Model::where($whereClause)->orWhere($orWhereClause)

}
else {
    $whereClause= ['mID' => $member->mID, 'qid' => $listing->lID, 'deleted' => false];
    $collection = Model::where($whereClause)

}

除了缺少;,我有什么想法可以实现这种where条件吗;,我认为你应该这样做:

$whereClause=['mID',NULL],'qid',$listing->lID],'deleted',false]; $orWhereClause=['mID',0],'qid',$listing->lID],'deleted',false]; $collection=Model::where$whereClause->orWhere$orWhereClause;
如果需要其他条件,只需在右侧数组中按一个数组,其中第一个参数是字段,第二个参数是值,或者第一个参数是字段,第二个参数是运算符,第三个参数是值,除了缺少;,我认为你应该这样做:

$whereClause=['mID',NULL],'qid',$listing->lID],'deleted',false]; $orWhereClause=['mID',0],'qid',$listing->lID],'deleted',false]; $collection=Model::where$whereClause->orWhere$orWhereClause;
如果需要其他条件,只需在右侧数组中按一个数组,其中第一个参数是字段,第二个参数是值,或者第一个参数是字段,第二个参数是运算符,第三个参数是值,这似乎是最简单的方法。将方法链接到其中与将它们作为数组传入相同

$collection = Model::where('qid', $listing->lID)->where('deleted', false);

if($condition === 1) {
    $collection = $collection->whereIn('mID', [0, NULL]);
}
else {
    $collection = $collection->where('mID', $member->mID);
}

请注意,如果要将数组传递给where方法,那么就错了。

这似乎是最简单的方法。将方法链接到其中与将它们作为数组传入相同

$collection = Model::where('qid', $listing->lID)->where('deleted', false);

if($condition === 1) {
    $collection = $collection->whereIn('mID', [0, NULL]);
}
else {
    $collection = $collection->where('mID', $member->mID);
}

请注意,如果要将数组传递给where方法,则操作错误。

Miken32是正确的,但代码可以在单个查询中编写,而不是在if/else语句中添加where:

$collection = Model::where('qid', $listing->lID)->where('deleted', false)
->when($condition === 1, function($q){
    $q->whereIn('mID', [0, NULL]);
})->when($condition !== 1, function($q){
    $q->where('mID', $member->mID);
});

Miken32是正确的,但代码可以在单个查询中编写,而不是在if/else语句中添加where:

$collection = Model::where('qid', $listing->lID)->where('deleted', false)
->when($condition === 1, function($q){
    $q->whereIn('mID', [0, NULL]);
})->when($condition !== 1, function($q){
    $q->where('mID', $member->mID);
});

这将包括mID=0的所有记录,无论是deleted=false还是qid=$linting->lID。@miken32已编辑,请尝试并告诉我发生了什么这将包括mID=0的所有记录,无论deleted=false还是qid=$linting->lID。@miken32已编辑,请尝试并告诉我发生了什么