简体   繁体   English

Laravel 控制器一次返回合并集合

[英]Laravel controller merge collections in one return

I have 2 queries to return data from DB by Laravel controller first :我有 2 个查询首先通过 Laravel 控制器从数据库返回数据:

$results = DB::table('transaction_sell_lines as tsl')
        ->select(DB::raw('b.name as results_business_location_name , t.location_id as results_location_id , tsl.product_id as results_product_id , p.name as results_product_name , tsl.variation_id as results_variation_id , sum(tsl.quantity) AS results_quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS results_total'))
        ->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
        ->leftjoin('business_locations as b','b.id', '=', 't.location_id')
        ->leftjoin('products as p','p.id', '=', 'tsl.product_id')
        ->whereBetween('t.transaction_date',[$startDate.' 00:00:00', $endDate.' 23:59:59'])
        ->where('b.custom_field1', $location_type)
        ->where('t.type', '=' , 'sell')
        ->where('t.status', '=' , 'final')
        ->where('p.id', '!=' , 1)
        ->groupBy('tsl.variation_id')
        ->orderBy('tsl.variation_id', 'asc')
        ->get();

it will return result like它会返回类似的结果

  #items: array:2 [▼
0 => { ▼
  +"results_business_location_name": "Video Game"
  +"results_location_id": 18
  +"results_product_id": 513
  +"results_product_name": "موتوسيكل"
  +"results_variation_id": 661
  +"results_quantity": "211.0000"
  +"results_total": "4220.00000000"
}
1 => { ▼
  +"results_business_location_name": "Video Game"
  +"results_location_id": 19
  +"results_product_id": 513
  +"results_product_name": "موتوسيكل"
  +"results_variation_id": 661
  +"results_quantity": "211.0000"
  +"results_total": "4220.00000000"
}

] and second query is : ],第二个查询是:

$results2 = DB::table('transaction_sell_lines as tsl')
        ->select(DB::raw('b.name as results2_business_location_name , t.location_id as results2_location_id , tsl.product_id as results2_product_id , p.name as results2_product_name , tsl.variation_id as results2_variation_id , sum(tsl.quantity) AS results2_quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS results2_total'))
        ->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
        ->leftjoin('business_locations as b','b.id', '=', 't.location_id')
        ->leftjoin('products as p','p.id', '=', 'tsl.product_id')
        ->whereRaw("t.transaction_date LIKE '$reportdate%'")
        ->where('b.custom_field1', $location_type)
        ->where('t.type', '=' , 'sell')
        ->where('t.status', '=' , 'final')
        ->where('p.id', '!=' , 1)
        ->groupBy('tsl.variation_id')
        ->orderBy('tsl.variation_id', 'asc')
        ->orderBy('b.id', 'asc')
        ->get(); 

it will return result like它会返回类似的结果

array:2 [▼
0 => { ▼
  +"results2_business_location_name": "Video Game"
  +"results2_location_id": 18
  +"results2_product_id": 513
  +"results2_product_name": "موتوسيكل"
  +"results2_variation_id": 661
  +"results2_quantity": "1.0000"
  +"results2_total": "20.00000000"
}
1 => { ▼
  +"results2_business_location_name": "Video Game"
  +"results2_location_id": 20
  +"results2_product_id": 513
  +"results2_product_name": "موتوسيكل"
  +"results2_variation_id": 661
  +"results2_quantity": "1.0000"
  +"results2_total": "20.00000000"
}

] ]

How can I merge between $results and $results2 to return only one collection , the marge must be to add object from $results2 to results which has same location_id and variation_id , and if not find in $results , It will be added alone example of needed collection如何在 $results 和 $results2 之间合并以仅返回一个集合,必须将对象从 $results2 添加到具有相同 location_id 和variation_id 的结果中,如果在 $results 中找不到,它将单独添加示例需要收集

#items: array:4 [▼
0 => { ▼
  +"results_business_location_name": "Video Game"
  +"results_location_id": 18
  +"results_product_id": 513
  +"results_product_name": "موتوسيكل"
  +"results_variation_id": 661
  +"results_quantity": "211.0000"
  +"results_total": "4220.00000000"
  +"results2_business_location_name": "Video Game"
  +"results2_location_id": 18
  +"results2_product_id": 513
  +"results2_product_name": "موتوسيكل"
  +"results2_variation_id": 661
  +"results2_quantity": "1.0000"
  +"results2_total": "20.00000000"
}
1 => { ▼
  +"results_business_location_name": "Video Game"
  +"results_location_id": 19
  +"results_product_id": 513
  +"results_product_name": "موتوسيكل"
  +"results_variation_id": 661
  +"results_quantity": "211.0000"
  +"results_total": "4220.00000000"
  +"results2_business_location_name": "0"
  +"results2_location_id": 0
  +"results2_product_id": 0
  +"results2_product_name": "0"
  +"results2_variation_id": 0
  +"results2_quantity": "0"
  +"results2_total": "0"
}
2 => { ▼
  +"results_business_location_name": "0"
  +"results_location_id": 0
  +"results_product_id": 0
  +"results_product_name": "0"
  +"results_variation_id": 0
  +"results_quantity": "0"
  +"results_total": "0"
  +"results2_business_location_name": "Video Game"
  +"results2_location_id": 20
  +"results2_product_id": 513
  +"results2_product_name": "موتوسيكل"
  +"results2_variation_id": 661
  +"results2_quantity": "1.0000"
  +"results2_total": "20.00000000"
}

] ]

Thanks in advance提前致谢

我认为这可能会通过使用数组合并来帮助您,例如:

array_merge($data1, $data2)

In Laravel you can merge two collection results by:在 Laravel 中,您可以通过以下方式合并两个集合结果:

$first_collection  = collect(['Dog']);

$second_collection = collect(['Cat']);

$merged_collection = $first_collection->merge($second_collection);

dd($merged_collection->toArray());

// ['Dog', 'Cat']

As in both quires you select the same columns, you can combine those quires into one which will return those results in one collection.就像在两个查询中选择相同的列一样,您可以将这些查询合并为一个,将这些结果返回到一个集合中。 You need to use UNION operator.您需要使用UNION运算符。

$first = DB::table('transaction_sell_lines as tsl')
    ->select(DB::raw('b.name as results_business_location_name , t.location_id as results_location_id , tsl.product_id as results_product_id , p.name as results_product_name , tsl.variation_id as results_variation_id , sum(tsl.quantity) AS results_quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS results_total'))
    ->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
    ->leftjoin('business_locations as b','b.id', '=', 't.location_id')
    ->leftjoin('products as p','p.id', '=', 'tsl.product_id')
    ->whereBetween('t.transaction_date',[$startDate.' 00:00:00', $endDate.' 23:59:59'])
    ->where('b.custom_field1', $location_type)
    ->where('t.type', '=' , 'sell')
    ->where('t.status', '=' , 'final')
    ->where('p.id', '!=' , 1)
    ->groupBy('tsl.variation_id')
    ->orderBy('tsl.variation_id', 'asc');

$result = DB::table('transaction_sell_lines as tsl')
    ->select(DB::raw('b.name as results2_business_location_name , t.location_id as results2_location_id , tsl.product_id as results2_product_id , p.name as results2_product_name , tsl.variation_id as results2_variation_id , sum(tsl.quantity) AS results2_quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS results2_total'))
    ->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
    ->leftjoin('business_locations as b','b.id', '=', 't.location_id')
    ->leftjoin('products as p','p.id', '=', 'tsl.product_id')
    ->whereRaw("t.transaction_date LIKE '$reportdate%'")
    ->where('b.custom_field1', $location_type)
    ->where('t.type', '=' , 'sell')
    ->where('t.status', '=' , 'final')
    ->where('p.id', '!=' , 1)
    ->groupBy('tsl.variation_id')
    ->orderBy('tsl.variation_id', 'asc')
    ->orderBy('b.id', 'asc')
    ->union($first)
    ->get(); 

you might need to modify this query, I haven't run it but should work.您可能需要修改此查询,我没有运行它但应该可以工作。

docs文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM