Php 使用单where条件的Laravel多通关系过滤器
我有一组用户,客户和他们是相关的,它将每个月改变,我的要求是获得活跃的客户月用户 我的客户模型Php 使用单where条件的Laravel多通关系过滤器,php,laravel,Php,Laravel,我有一组用户,客户和他们是相关的,它将每个月改变,我的要求是获得活跃的客户月用户 我的客户模型 public function executives() { return $this->belongsToMany(User::class, 'user_customer_mapping', 'user_customer_mapping_customer_id', 'user_customer_mapping_user_id'); } user\u cu
public function executives()
{
return $this->belongsToMany(User::class, 'user_customer_mapping', 'user_customer_mapping_customer_id', 'user_customer_mapping_user_id');
}
user\u customer\u mapping表中有一列表示月份的值,如(Y-m),我需要为所有客户找到“2020-10”高管
$month= '2020-10';
$Executive = $customer->executives()->where('user_farmer_mapping_season', $month)->first();
return ($fieldExecutive) ? $fieldExecutive->name : 'N/A';
首先添加到关系中,以便同时加载其他数据透视表列:
public function executives()
{
return $this->belongsToMany(User::class, 'user_customer_mapping', 'user_customer_mapping_customer_id', 'user_customer_mapping_user_id')
->withPivot('month');
}
接下来,使用查询透视表列:
$month= '2020-10';
$Executive = $customer->executives()->wherePivot('month', $month)->first();
return ($fieldExecutive) ? $fieldExecutive->name : 'N/A';