加入两个 Laravel 集合

Ril*_*zzz 3 php collections laravel

我有两个 Laravel 集合。第一个集合 $customer(它有 30 个元素):

Collection {#2615 ?
  #items: array:31 [?
    0 => {#2610 ?
      +"allocated_date": "2016-12-01"
      +"Customer": "44"
    }
    1 => {#2616 ?
      +"allocated_date": "2016-12-02"
      +"Customer": "42"
    }
Run Code Online (Sandbox Code Playgroud)

另一个是 $agent(它有 17 个元素)

Collection {#2586 ?
  #items: array:16 [?
    0 => {#2585 ?
      +"agent_allocated_date": "2016-12-01"
      +"Agent": "41"
    }
    1 => {#2587 ?
      +"agent_allocated_date": "2016-12-02"
      +"Agent": "95"
    }
Run Code Online (Sandbox Code Playgroud)

我需要这样的结果(leftJoinlocated_date 和 agent_allocated_date)。不能使用合并或组合。因为两个集合中的元素数量不同。帮我找到输出

array:31 [?
      0 => {#2596 ?
        +"allocated_date": "2016-12-01"
        +"Customer": "44"
        +"agent_allocated_date": "2016-12-01"
        +"Agent": "41"
      }
Run Code Online (Sandbox Code Playgroud)

mad*_*scu 5

您需要映射客户并找到该客户的代理并合并两个集合:

$customer= $customer->map(function ($item, $key) {
    $single_agent = $agent->where('agent_allocated_date',$item->agent_allocated_date);
    return collect($item)->merge($single_agent);
});
Run Code Online (Sandbox Code Playgroud)