Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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
Php 使用单where条件的Laravel多通关系过滤器_Php_Laravel - Fatal编程技术网

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';