Tried helping you on the last thread, not again @lovercode
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
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"
] ]
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.