加入两个laravel系列

问题描述 投票:1回答:3

我有两个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"
    }

另一个是$ 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"
    }

我需要这样的结果(leftJoin allocated_date with agent_allocated_date)。无法使用合并或合并。因为两个集合中的元素数量不同。帮我找到输出

array:31 [▼
      0 => {#2596 ▼
        +"allocated_date": "2016-12-01"
        +"Customer": "44"
        +"agent_allocated_date": "2016-12-01"
        +"Agent": "41"
      }
php laravel collections
3个回答
3
投票

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

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

0
投票
$result = [];    

foreach($collection as $datas){

   foreach($datas as $data){
      $result[] = $data;
   }

 }

0
投票

执行连接查询的更好方法。我已经测试了这个连接查询。它正在发挥作用。试试这个吧。

$data = DB::table('customer')->select('customer.allocated_date','customer.Customer','agent.agent_allocated_date','agent.Agent')->join('agent','agent.id','=','customer.id');

print_r($data);
© www.soinside.com 2019 - 2024. All rights reserved.