对象数组唯一 - Laravel 5.0

时间:2016-08-12 09:51:04

标签: php arrays laravel-5 unique

我有以下对象数组:

array:6 [▼
  0 => {#445 ▼
    +"prod_id": "276"
    +"prod_name": "Vanish"
    +"prod_code": "B-V-001"
    +"prod_price": "80.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-B-V-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "4"
    +"ord_prd_code": "B-V-001"
    +"ord_code": "ORD-000000351"
    +"ord_prd_opt_code": ""
  }
  1 => {#498 ▼
    +"prod_id": "276"
    +"prod_name": "Vanish"
    +"prod_code": "B-V-001"
    +"prod_price": "80.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-B-V-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "4"
    +"ord_prd_code": "B-V-001"
    +"ord_code": "ORD-000000350"
    +"ord_prd_opt_code": ""
  }
  2 => {#499 ▼
    +"prod_id": "276"
    +"prod_name": "Vanish"
    +"prod_code": "B-V-001"
    +"prod_price": "80.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-B-V-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "4"
    +"ord_prd_code": "B-V-001"
    +"ord_code": "ORD-000000349"
    +"ord_prd_opt_code": ""
  }
  3 => {#500 ▼
    +"prod_id": "272"
    +"prod_name": "Clear Orchid Pot + Orchid Boon "
    +"prod_code": "O-013"
    +"prod_price": "110.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-o-013"
    +"prod_enquiry": "No"
    +"prod_quantity": "4"
    +"ord_prd_code": "O-013"
    +"ord_code": "ORD-000000348"
    +"ord_prd_opt_code": ""
  }
  4 => {#501 ▼
    +"prod_id": "45"
    +"prod_name": "Coir mulch mat"
    +"prod_code": "CP-MM-001"
    +"prod_price": "20.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-cp-mm-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "14"
    +"ord_prd_code": "CP-MM-001"
    +"ord_code": "ORD-000000348"
    +"ord_prd_opt_code": ""
  }
  5 => {#502 ▼
    +"prod_id": "10"
    +"prod_name": "Biogreen"
    +"prod_code": "F-Bi-001"
    +"prod_price": "80.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-f-bi-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "5"
    +"ord_prd_code": "F-Bi-001"
    +"ord_code": "ORD-000000348"
    +"ord_prd_opt_code": ""
  }
  // and the list continues upto latest 18 results...
]

我想要上面数组中的唯一对象..所以,所需的输出是:

array:4 [▼
  0 => {#445 ▼
    +"prod_id": "276"               // <== Earlier 3, now 1.
    +"prod_name": "Vanish"
    +"prod_code": "B-V-001"
    +"prod_price": "80.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-B-V-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "4"
    +"ord_prd_code": "B-V-001"
    +"ord_code": "ORD-000000351"
    +"ord_prd_opt_code": ""
  }
  1 => {#500 ▼
    +"prod_id": "272"
    +"prod_name": "Clear Orchid Pot + Orchid Boon "
    +"prod_code": "O-013"
    +"prod_price": "110.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-o-013"
    +"prod_enquiry": "No"
    +"prod_quantity": "4"
    +"ord_prd_code": "O-013"
    +"ord_code": "ORD-000000348"
    +"ord_prd_opt_code": ""
  }
  2 => {#501 ▼
    +"prod_id": "45"
    +"prod_name": "Coir mulch mat"
    +"prod_code": "CP-MM-001"
    +"prod_price": "20.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-cp-mm-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "14"
    +"ord_prd_code": "CP-MM-001"
    +"ord_code": "ORD-000000348"
    +"ord_prd_opt_code": ""
  }
  3 => {#502 ▼
    +"prod_id": "10"
    +"prod_name": "Biogreen"
    +"prod_code": "F-Bi-001"
    +"prod_price": "80.00"
    +"prod_disc_price": "0.00"
    +"prod_disc_percent": "0.00"
    +"image_code": "img-f-bi-001"
    +"prod_enquiry": "No"
    +"prod_quantity": "5"
    +"ord_prd_code": "F-Bi-001"
    +"ord_code": "ORD-000000348"
    +"ord_prd_opt_code": ""
  }

我有以下代码:

$recentlySoldProducts = DB::table('products')
        ->join('orders', 'orders.product_id', '=', 'products.id')
        ->where('products.display', '=', 'Enabled')
        ->where('products.deleted_at', '=', NULL)
        ->select(
            'products.id AS prod_id',
            'products.name AS prod_name',
            'products.code AS prod_code',
            'products.price AS prod_price',
            'products.discount_price AS prod_disc_price',
            'products.discount_percent AS prod_disc_percent',
            'products.img_code AS image_code',
            'products.enquire AS prod_enquiry',
            'products.quantity AS prod_quantity',
            'orders.product_code AS ord_prd_code',
            'orders.order_code AS ord_code',
            'orders.product_option_code AS ord_prd_opt_code',
            'orders.id AS order_id',
            'orders.product_id AS order_product_id'
        )->groupBy(['order_product_id', 'ord_code'])
         ->orderBy('orders.id', 'desc')->take(18)->get();

我想要实现的是最近销售的产品块。无论何时下订单,这些产品最近都会在这里出售。如果1个产品以不同的订单多次出售,我想合并该产品并将其作为1个产品,而不是重复产品,否则按原样获取订购的产品。

我应该如何实现这一目标?感谢。

我正在使用Laravel 5.0。*用于此应用程序..

1 个答案:

答案 0 :(得分:0)

您可以在商品ID上添加GROUP BY,这样就不会添加任何重复项。

使用查询构建器,它看起来像这样:

$recentlySoldProducts = DB::table('products')
            ->join('...')
            ->where('...')
            ->select('...')
            ->groupBy('products.id')
            ->orderBy('orders.id', 'desc')->limit(18)->get();