У меня есть 2 коллекции. Первая коллекция создается запросом с условием where, а вторая коллекция - всеми элементами. Я намерен объединить две коллекции, но между первым массивом и вторым массивом есть одинаковые элементы. Как сделать элементы уникальными, когда они объединены?
Вот первый массив,
[
{
"id": 71,
"id_brand": 1,
"id_brand_outlet": 14,
"id_user": 5,
"id_brand_outlet_tujuan": 15,
"alasan": "Try",
"no_surat": "JJ-00001-1",
"created_at": "2020-03-10 08:57:00",
"updated_at": "2020-03-10 08:57:00",
"type": "Delivery Out",
"category": "ITEM MASUK",
"brand_items": [
{
"id": 1,
"id_brand": 1,
"id_brand_delivery": 71,
"id_brand_item": 1,
"id_unit": 2,
"qty_before": 10,
"qty_change": 10,
"qty_after": 0,
"created_at": "2020-03-10 08:57:00",
"updated_at": "2020-03-10 08:57:00",
"name": "ALPUKAT",
"id_unit_opname": 2,
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 8,
"id_brand": 1,
"id_brand_delivery": 71,
"id_brand_item": 8,
"id_unit": 2,
"qty_before": 10,
"qty_change": 10,
"qty_after": 0,
"created_at": "2020-03-10 08:57:00",
"updated_at": "2020-03-10 08:57:00",
"name": "GULA AREN",
"id_unit_opname": 2,
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
}
]
}
]
А вот второй массив
[
{
"id": 1,
"id_owner": 3,
"name": "Janji Jiwa",
"slug": "JJ",
"logo": "images/brand/4041569999956.png",
"created_at": null,
"updated_at": "2020-03-05 16:02:11",
"category": "ITEM LAINNYA",
"brand_items": [
{
"id": 1,
"id_brand": 1,
"id_brand_category": 1,
"name": "ALPUKAT",
"accurate_name": "ALPUKAT",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 25000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": "2",
"created_at": "2020-02-25 09:32:46",
"updated_at": "2020-02-25 09:32:46",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 3,
"id_brand": 1,
"id_brand_category": 8,
"name": "CHOCOLATE",
"accurate_name": "CHOCOLATE",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 25000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:47",
"updated_at": "2020-02-25 09:32:47",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 8,
"id_brand": 1,
"id_brand_category": 9,
"name": "GULA AREN",
"accurate_name": "GULA AREN",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 12000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 9,
"id_brand": 1,
"id_brand_category": 12,
"name": "PLASTIC CUP 14 OZ",
"accurate_name": "PLASTIC CUP 14 OZ",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 17000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 11,
"id_brand": 1,
"id_brand_category": 8,
"name": "ROTI TAWAR",
"accurate_name": "ROTI TAWAR",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 25000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 14,
"id_brand": 1,
"id_brand_category": 13,
"name": "TISSUE GULUNG",
"accurate_name": "TISSUE GULUNG",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 17000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
}
]
}
]
А вот мой код:
public function deliveryIn(Request $request){
$post = $request->all();
$data1 = BrandOutletDelivery::where('no_surat', 'LIKE' ,'%'.$post['no_surat'])->where('type', 'Delivery Out')->whereDate('created_at', date("Y-m-d"))->with('brandOutletDeliveryItems.unit')->get();
if(count($data1) > 0){
$data1[0]['category']="ITEM MASUK"; //add key & value to the array
foreach($data1 as $key => $value){
foreach ($value['brandOutletDeliveryItems'] as $key1 => $val) {
$value['brandOutletDeliveryItems'][$key1]['name'] = $val['brandItem']['name'];
$value['brandOutletDeliveryItems'][$key1]['id'] = $val['brandItem']['id'];
$value['brandOutletDeliveryItems'][$key1]['id_unit_opname'] = $val['brandItem']['id_unit_opname'];
$val['unit_opname'] = $val['unit'];
unset($val['unit']);
unset($val['brandItem']);
// $data2 = $data2->get();
}
$value['brand_items'] = $value['brandOutletDeliveryItems'];
unset($data1[$key]['brandOutletDeliveryItems']);
}
}
$data2 = Brand::where('id', $post['id_brand']);
$data2->with(['brandItems' => function($q){
$q->where('is_inventory', 1)->with('unitOpname');
}]);
$data2 = $data2->get();
// }
$data2[0]['category']="ITEM LAINNYA"; //add key & value to the array
$data = $data1->merge($data2);
return response(MyHelper::checkGet($data));
return ['status' => 'fail', 'messages' => ['Nomor Surat Tidak Ditemukan']];
}
В первом массиве есть brand_items
с именами "ALPUKAT" и "GULA AREN", и те же элементы находятся также во втором массиве. Как я могу сделать так, чтобы элементы в первом массиве больше не появлялись во втором массиве? Я попытался объединить два массива, но все же элементы не являются уникальными.