Skip to content
Advertisement

Larave error: array_merge(): Expected parameter 2 to be an array, string given

Hi fellow Laravel developers: The following is the output of dd(session(‘cart’)); :

array:2 [▼
  17 => array:14 [▼
    "user_id" => 1
    "nr" => "001"
    "check_id" => 17
    "kommission" => "Ribnev Prömski"
    "quantity" => 1
    "price" => "72.95"
    "shipping_cost_id" => 28
 
  ]
  18 => array:14 [▼
    "user_id" => 1
    "nr" => "002"
    "check_id" => 17
    "kommission" => "Ribnev Prömski"
    "quantity" => 1
    "price" => "78.95" 
    "shipping_cost_id" => 28
 
  ]
]

Later on id like to save the cart as single rows in the orders table. In preparation of this … When i do the following:

foreach (session('cart') as $id => $data ) {
    dump($data['nr']);
}

The Browser shows (very short) the first iteration of the foreach “001” ($data[‘nr’]), and then I get this Error:

array_merge(): Expected parameter 2 to be an array, string given.

My Question: What is meant by parameter 2?

Advertisement

Answer

You can do by this:

$data = collect(session('cart'))->map(function ($item, $key) {
           return $item['nr'];
        });

Another way:

You can use pluck the key you want:

$collection = collect(session('cart'));

$data = $collection->pluck('nr')
       ->all();

More information with collection you can check it at here:

https://laravel.com/docs/8.x/collections

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement