Php Laravel 5.8 withCount()忽略我的where语句
我试图找到代理系统中候选人的具体合规项的数量 当我使用with()时,它返回1个正确的结果,但当我使用withCount()时,它返回所有符合项的数量,并忽略where语句 候选模型:Php Laravel 5.8 withCount()忽略我的where语句,php,laravel,Php,Laravel,我试图找到代理系统中候选人的具体合规项的数量 当我使用with()时,它返回1个正确的结果,但当我使用withCount()时,它返回所有符合项的数量,并忽略where语句 候选模型: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Candidate extends Model { protected $table = 'candidates'; public function
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Candidate extends Model
{
protected $table = 'candidates';
public function complianceItemsActive()
{
return $this->hasMany('App\CandidateComplianceItem', 'candidate_id', 'id')
->where('valid_until', '>=', date('Y-m-d'))
->orWhereNull('valid_until');
}
}
我可能错了,但我想这可能和你们关系中的附加条款有关。我建议将这些条款嵌套,以便它们不会与您稍后添加的条款冲突:
public function complianceItemsActive()
{
return $this->hasMany('App\CandidateComplianceItem', 'candidate_id', 'id')
->where(function ($query) {
$query->where('valid_until', '>=', date('Y-m-d'))->orWhereNull('valid_until');
});
}
我可能错了,但我想这可能和你们关系中的附加条款有关。我建议将这些条款嵌套,以便它们不会与您稍后添加的条款冲突:
public function complianceItemsActive()
{
return $this->hasMany('App\CandidateComplianceItem', 'candidate_id', 'id')
->where(function ($query) {
$query->where('valid_until', '>=', date('Y-m-d'))->orWhereNull('valid_until');
});
}
public function complianceItemsActive()
{
return $this->hasMany('App\CandidateComplianceItem', 'candidate_id', 'id')
->where(function ($query) {
$query->where('valid_until', '>=', date('Y-m-d'))->orWhereNull('valid_until');
});
}