Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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 使用数组值检索数据-laravel_Php_Laravel - Fatal编程技术网

Php 使用数组值检索数据-laravel

Php 使用数组值检索数据-laravel,php,laravel,Php,Laravel,我的任务是显示选定组的所有联系人。当我选择facebook和instagram等两个Gorup时,我必须显示属于这两个组的所有联系人 当我选择下面这样的多个组并执行返回$explode\u groups时,我将获得两个组的id,如1,2 但我的问题是,当我将联系人显示为return$selected\u contacts时,我只会获得组1的联系人 为什么会这样 集团 public function customers() { return $this->belon

我的任务是显示选定组的所有联系人。当我选择facebook和instagram等两个Gorup时,我必须显示属于这两个组的所有联系人

当我选择下面这样的多个组并执行
返回$explode\u groups
时,我将获得两个组的id,如
1,2

但我的问题是,当我将联系人显示为
return$selected\u contacts
时,我只会获得组1的联系人

为什么会这样

集团

 public function customers()
    {
        return $this->belongsToMany('App\Customer','customer_group','group_id','customer_id')
        ->withTimestamps();
    }
客户

 public function groups()
    {
        return $this->belongsToMany('App\Group','customer_group','customer_id','group_id')
        ->withTimestamps();
    }
$get_selected_groups = $request->get('group');

$explode_groups = implode(', ', $get_selected_groups);

$selected_groups = Group::where('id',$explode_groups)->first();

$selected_contacts = $selected_groups->customers()->get();
控制器

 public function groups()
    {
        return $this->belongsToMany('App\Group','customer_group','customer_id','group_id')
        ->withTimestamps();
    }
$get_selected_groups = $request->get('group');

$explode_groups = implode(', ', $get_selected_groups);

$selected_groups = Group::where('id',$explode_groups)->first();

$selected_contacts = $selected_groups->customers()->get();
响应

{"id":2,"title":"test","no_of_contacts":0,"user_id":1,"created_at":"2018-04-15 23:55:30","updated_at":"2018-04-15 23:55:30","customers":[{"id":1,"name":"Benson Jones Thomson","phone":"0247878234","group_id":null,"user_id":1,"created_at":"2018-04-16 00:14:20","updated_at":"2018-04-16 05:31:05","pivot":{"group_id":2,"customer_id":1,"created_at":"2018-04-16 05:33:08","updated_at":"2018-04-16 05:33:08"}},{"id":2,"name":"Lawrence Pitcher","phone":"0244371112","group_id":null,"user_id":1,"created_at":"2018-04-16 07:59:15","updated_at":"2018-04-16 07:59:15","pivot":{"group_id":2,"customer_id":2,"created_at":"2018-04-16 07:59:15","updated_at":"2018-04-16 07:59:15"}}]}

您应该使用
where()
从给定数组中选择多个ID,并使用
with()
方法加载每个模型的关系

比如:

试试这个

$selected_contacts = Customer::whereHas('groups', function($q) use ($explode_groups){
            $q->whereIn('id', $explode_groups);
})->get();
你应该试试这个

$selected_groups = Group::whereIn('id',$get_selected_groups)->get();

$selected_contacts = array();

foreach($selected_groups as $selected_group){

   $selected_contacts[] = $selected_group->customers()->get();

}

Group::其中('id',$request->get('Group')
,你应该停止做你正在做的事情,拿起一本书或看一些关于编程的系列,特别是SQL。向我们展示你的客户和组模型及其关系function@Kyslik,请在评论之前阅读问题。因为我试过你的基本功answer@Sohel0415,model updated Now显示传递给Lightning\Database\Grammar::parameterize()的$get\u selected\u groupsArgument 1的转储必须是数组类型,字符串给定,请检查问题中的响应。。我正试图得到客户的财产