I'm trying to create a product label. While I create an array and pass it to the blade it's not working.
-> Controller
$item_name = $request->input('item_name');
$label_quantity = $request->input('label_quantity');
foreach ($item_name as $key => $name) {
$product = Product::find($item_name[$key]);
$barcode = Stock::where('product_id', '=', $item_name[$key])->get();
$data = [
'name' => $product->name,
'price' => $product->sale_price,
'barcode' => $barcode[0]['barcode'],
'quantity' => $label_quantity[$key],
];
return back()->with(compact('data'));
}
-> Blade File
@php
if (isset($data)) {
print_r(json_encode($data));
} else {
echo 'No Data Available';
}
@endphp
CodePudding user response:
Please specify, what is inside the $item_name variable. Does it contain multiple products data?
If it contains only 1 product data then you change your code like this:
$item_name = $request->input('item_name');
$label_quantity = $request->input('label_quantity');
$product = Product::find($item_name[0]);
$barcode = Stock::where('product_id', '=', $item_name[0])->get();
$data = [
'name' => $product->name,
'price' => $product->sale_price,
'barcode' => $barcode[0]['barcode'],
'quantity' => $label_quantity[0],
];
return redirect('/abc', compact('data'));
Here you are redirecting to '/abc' route with $data array. You can also render a view and pass data to it like this:
return view('home', compact('data'));
CodePudding user response:
return view('your view', ['data' => $data]);
if you want to pass many use compact built in function
return view('view name', compact(['data', 'data2', 'data3']));