我有一组用户,客户和他们是相关的,而且每个月都会有变化,我的要求是为客户获得活跃的月份用户。
我的客户模型
public function executives()
{
return $this->belongsToMany(User::class, 'user_customer_mapping', 'user_customer_mapping_customer_id', 'user_customer_mapping_user_id');
}user_customer_mapping表有一个月列,其值类似于(Y-m),我需要为所有客户找到"2020-10“执行。
$month= '2020-10';
$Executive = $customer->executives()->where('user_farmer_mapping_season', $month)->first();
return ($fieldExecutive) ? $fieldExecutive->name : 'N/A';发布于 2020-11-25 05:50:07
首先,将withPivot添加到关系中,以便加载附加的枢轴表列:
public function executives()
{
return $this->belongsToMany(User::class, 'user_customer_mapping', 'user_customer_mapping_customer_id', 'user_customer_mapping_user_id')
->withPivot('month');
}接下来,使用wherePivot对枢轴表列进行查询:
$month= '2020-10';
$Executive = $customer->executives()->wherePivot('month', $month)->first();
return ($fieldExecutive) ? $fieldExecutive->name : 'N/A';https://stackoverflow.com/questions/64999105
复制相似问题