Combine array objects in PHP using Eloquent

I have a collection of objects extracted from my database using the Laravel Rloquent ORM (I do not use Laravel only integrated Eloquent in my own Framework). I used the transform method to iterate over the collection and unserializeone of the columns of each record, then collapsed the collection to put all unserialized objects into one array.

Here is the logic:

  $orders = Order::where('user', $user)->orderBy('id', 'desc')->get();

  $orders->transform(function($order, $key) {

    $order->cart = unserialize($order->cart);

    $items = $order->cart->items;

    return $items;
  });

  $collapsed = $orders->collapse();

And the conclusion:

[
   {
      "qty": "2",
      "price": 200,
      "item": {
         "id": 1,
         "title": "Black Hoodie",
         "img": "https://s3.amazonaws.com/bucket/black-hoodie.jpg",
         "description": "Plain Black Hoodie.",
         "price": 100
      }
   },
   {
      "qty": "2",
      "price": 200,
      "item": {
         "id": 2,
         "title": "Green Hoodie",
         "img": "https://s3.amazonaws.com/bucket/green-hoodie.jpg",
         "description": "Plain Green Hoodie.",
         "price": 100
      }
   },
   {
      "qty": 1,
      "price": 100,
      "item": {
         "id": 2,
         "title": "Green Hoodie",
         "img": "https://s3.amazonaws.com/bucket/green-hoodie.jpg",
         "description": "Plain Green Hoodie.",
         "price": 100
      }
   },
   {
      "qty": 1,
      "price": 100,
      "item": {
         "id": 1,
         "title": "Black Hoodie",
         "img": "https://s3.amazonaws.com/bucket/black-hoodie.jpg",
         "description": "Plain Black Hoodie.",
         "price": 100
      }
   }
]

Now I want to do the following: to combine all the same objects in this array, ideally, by their value "item":{"id"}, into one object - adding their properties qtyand pricetogether leaving the itemproperty the same.

My desired result will be

[
   {
      "qty": "3",
      "price": 300,
      "item": {
         "id": 1,
         "title": "Black Hoodie",
         "img": "https://s3.amazonaws.com/bucket/black-hoodie.jpg",
         "description": "Plain Black Hoodie.",
         "price": 100
      }
   },
   {
      "qty": "3",
      "price": 300,
      "item": {
         "id": 2,
         "title": "Green Hoodie",
         "img": "https://s3.amazonaws.com/bucket/green-hoodie.jpg",
         "description": "Plain Green Hoodie.",
         "price": 100
      }
   }
]

Eloquent TONS , , , , .

+4
1

:

use Illuminate\Support\Fluent;
use Illuminate\Support\Collection;

// Mocking data to meet your requirements.
// Wrapping attributes inside fluent object,
// so we'll have almost same object like Eloquent model
$orders = new Collection([
    new Fluent([
        'qty' => 2,
        'price' => 200,
        'item' => new Fluent([
            'id' => 1,
            'price' => 100,
            'title' => 'Black Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/black-hoodie.jpg',
            'description' => 'Plain Black Hoodie.',
        ])
    ]),
    new Fluent([
        'qty' => 2,
        'price' => 200,
        'item' => new Fluent([
            'id' => 2,
            'price' => 100,
            'title' => 'Green Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/green-hoodie.jpg',
            'description' => 'Plain Green Hoodie.',
        ])
    ]),
    new Fluent([
        'qty' => 1,
        'price' => 100,
        'item' => new Fluent([
            'id' => 2,
            'price' => 100,
            'title' => 'Green Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/green-hoodie.jpg',
            'description' => 'Plain Green Hoodie.',
        ])
    ]),
    new Fluent([
        'qty' => 1,
        'price' => 100,
        'item' => new Fluent([
            'id' => 1,
            'price' => 100,
            'title' => 'Black Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/black-hoodie.jpg',
            'description' => 'Plain Black Hoodie.',
        ])
    ])
]);

// Here something you might interested
$result = $orders->groupBy('item.id')
    ->map(function ($orders, $itemId) {
        return new Fluent([
            'qty' => $orders->sum('qty'), // calculating quantity
            'price' => $orders->sum('price'), // calculating total price
            'item' => $orders->first()->item // leave the item as it is
        ]);
    });

// You may evaluate the result here
dd($result);

Update

"" :

use Illuminate\Support\Collection;

$orderOne = [
    'id' => 1,
    'price' => 100,
    'title' => 'Black Hoodie',
    'img' => 'https://s3.amazonaws.com/bucket/black-hoodie.jpg',
    'description' => 'Plain Black Hoodie.',
];

$orderTwo = [
    'id' => 2,
    'price' => 100,
    'title' => 'Green Hoodie',
    'img' => 'https://s3.amazonaws.com/bucket/green-hoodie.jpg',
    'description' => 'Plain Green Hoodie.',
];

$orders = new Collection([
    [
        'qty' => 2,
        'price' => 200,
        'item' => $orderOne
    ],
    [
        'qty' => 2,
        'price' => 200,
        'item' => $orderTwo
    ],
    [
        'qty' => 1,
        'price' => 100,
        'item' => $orderTwo
    ],
    [
        'qty' => 1,
        'price' => 100,
        'item' => $orderOne
    ]
]);

$result = $orders->groupBy('item.id')
    ->map(function ($orders, $itemId) {
        return [
            'qty' => $orders->sum('qty'),
            'price' => $orders->sum('price'),
            'item' => $orders->first()['item']
        ];
    });

dd($result);
+1

Source: https://habr.com/ru/post/1683311/


All Articles