Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
Mysql whereHas中的Laravel multiple where子句约束hasMany关系_Mysql_Laravel_Eloquent - Fatal编程技术网

Mysql whereHas中的Laravel multiple where子句约束hasMany关系

Mysql whereHas中的Laravel multiple where子句约束hasMany关系,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,我有以下疑问 $pgcs = PrivateGuard::with(['licences', 'state']) ->whereHas('licences', function($query){ $query->whereDate('expiration_of_licence', '<', Carbon::today()) ->where('renewal', 0); })

我有以下疑问

$pgcs = PrivateGuard::with(['licences', 'state'])
        ->whereHas('licences', function($query){
          $query->whereDate('expiration_of_licence', '<', Carbon::today())
                ->where('renewal', 0);
        })
        ->where('status', 1)
        ->get();
$pgcs=PrivateGuard::with(['licenses','state'])
->whereHas(‘许可证’、功能($query){
$query->whereDate('许可证到期','您的案例

  • [1] pgc..拥有两个或多个续期为0、1的许可证
  • [2] pgc..拥有两个或多个续期为0、0的许可证
  • [3] pgc..拥有两个或多个续期为1的许可证,1
现在,您的查询正在获取案例[1]、[2]的PGC

因此,您必须稍微修改一下查询逻辑,才能得到[2]

$pgcs = PrivateGuard::with(['licences', 'state'])
        ->whereHas('licences', function($query){
          $query->whereDate('expiration_of_licence', '<', Carbon::today())
                ->where('renewal', 0);
        })
        ->whereDoesntHave('licences', function($query){
          $query->where('renewal', 1);
        })
        ->where('status', 1)
        ->get();
$pgcs=PrivateGuard::with(['licenses','state'])
->whereHas(‘许可证’、功能($query){

$query->whereDate('许可证到期','哇!我从早上开始就在这个问题上拼命工作……谢谢艾哈迈德,它很有魅力。