Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lara_Love's avatar

seve product in array for invoice

hi all when i show invoice to user , it is a invoice include one user name and one user mobile and many products . how i can show invoce without foreach with foreach for each id show one invoice

id 	user_id 	product_id 	name 	price 	
1      	1 	      2 	    ssd 	     1000 	     
2 	    1       1 	    i9 gen12 	1000 	

@ foreach($invoice as $item)

{{ $item->user->name }}

{{ $item->name }}

{{ $item->price}}

thank you for your help

or How i can store data in array

store in session by

public function addToCart($id)

{

    $product = Product::findOrFail($id);
    $cart = session()->get('cart', []);
    if(isset($cart[$id])) {
        $cart[$id]['quantity']++;
    } else {
        $cart[$id] = [
            'user_id' => Auth::id(),
            'product_id' => $product->id,
            "name" => $product->name,
            "quantity" => 1,
            "price" => $product->price,
            "image" => $product->image
        ];
    }
    session()->put('cart', $cart);
    return redirect()->back();
}

Link send to store in database

Pay

public function ad(Request $request,$id)

{

   $products = $request->session()->get('cart',$id);

dd($products);

foreach ($products as $product) {
    Faktor::create(array[
        'user_id' => Auth::id(),
        'product_id' => $product['product_id'],
        'name' => $product['name'],
        'quantity' => $product['quantity'],
        'price' => $product['price'],
    ]);

}

    return redirect('bill');

}

and in dd

^ array:2 [▼

1 => array:6 [▼

"user_id" => 1
"product_id" => 1
"name" => "i9 gen 12"
"quantity" => "2"
"price" => "1000"
"image" => "20221016200852.jpg"

]

2 => array:6 [▼

"user_id" => 1
"product_id" => 2
"name" => "ssd"
"quantity" => 1
"price" => "1000"
"image" => "20221016200911.jpg"

] ]

0 likes
4 replies
Lara_Love's avatar

@tykus Hello my friend

I hope I can help, but the problem is that I don't know much. You have entered the site for help. It doesn't matter if you help me or someone else.

Lara_Love's avatar
Lara_Love
OP
Best Answer
Level 6

this should create like arraye that all product and quantity be one invoice

foreach ($products as $product)

{

    Faktor::create([
        'user_id' => Auth::id(),
        'product_id' => $product['product_id'],
        'name' => $product['name'],
        'quantity' => $product['quantity'],
        'price' => $product['price'],
    ]);
}

Please or to participate in this conversation.