Jubayer Alam
Jubayer Alam

Reputation: 29

Getting Array Data throught Loop on Laravel 8

I am using a form where it sends array data to the controller. and It also received array data like this

$product_unit = $inputs['orderProducts'];

enter image description here

but prob is when i pass this data through foreach loop for matching other products it just show one single array.

foreach ($product_unit as $unit){
        $product = Product::all()
            ->where('id', '=', $unit['product_id'])
            ->where('unit', '>=', $unit['quantity'])
            ->first();
    }

enter image description here

Here i Want to match received array data to existing product that are inserted. If it matched existing data it will be increment. But here it can only pass single array. Thats why its just increse one row value. Specially Last matched value is incresed. So How can that array data through loop by where function in laravel.

Upvotes: 1

Views: 1986

Answers (2)

Mo An Bl
Mo An Bl

Reputation: 56

the $product variable should be an array ;

$product[]

Upvotes: 0

A.A Noman
A.A Noman

Reputation: 5270

You have to use like below.

$product= array();

foreach ($product_unit as $unit){
    $product[] = Product::all()
        ->where('id', '=', $unit['product_id'])
        ->where('unit', '>=', $unit['quantity'])
        ->first();
}

dd($product)

Upvotes: 1

Related Questions