簡體   English   中英

檢查Laravel數據透視表中是否存在多行

[英]Check if multiple rows exist in Laravel pivot table

我可以運行以下代碼來檢查數據透視表中是否存在單行

 if($person->comments()->where('comment_id', 1)->exists()) == 1){
   //Do something
 }

但是,如何檢查行列表是否存在?

簡而言之,我想說的是:“數據透視表中是否包含具有id為1,2,3,4,5的comment_ids的行”。

這就是我到目前為止所得到的。

  if(($person->comments()->where('comment_id', 1)->where('comment_id', 2)->where('comment_id', 3)->where('comment_id', 4)->where('comment_id', 5)->exists()) == 1){
     //Do something
  }

如果您想將all ID與查詢進行匹配and則可以執行以下操作-

$query = $person->comments();
foreach($ids as $id){
    $query->where('comment_id',$id);
}
if(($query->exists()){
 //Do something
}

您可以計算有多少,例如:

$lookFor = [1,2,3,4];

if($person->comments()->whereIn('comment_id', $lookFor)->count() == count($lookFor)) { 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM