php - 检查 Laravel 数据透视表中是否存在多行

标签 php mysql laravel-5 pivot-table

我可以运行以下代码来检查数据透视表中是否存在单行

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

但是我如何检查行列表是否存在?

简单来说,我想说“如果数据透视表中的comment_ids行的id为1,2,3,4,5”。

这就是我到目前为止所得到的。

  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
  }

最佳答案

您可以数一下有多少个,例如:

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

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

关于php - 检查 Laravel 数据透视表中是否存在多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48386846/

相关文章:

php - 与 Doctrine PHP 的关联

php - 如何在没有cron的情况下向MySQL数据库写入数据?

php - 记录被检索两次

PHP 和 MYSQL - (mysqli_num_rows) 始终等于 0

php - Laravel:使用 `configureMonologUsing()` 的多个日志提供程序?

php - Guzzle PUT 请求身份验证错误

php - 多个客户表或大型整体表

mysql - 将微秒四舍五入为毫秒 MySQL

php - 将数组从一个表插入到另一个表

php - 更改日期格式 laravel 5