I am currently making an ecommerce site and am new to Laravel, and at the moment I'd like to retrieve data based on different categories from the same table, using a where clause to store the retrieved data in separate variables. The problem here is whenever I run this code it say undefined variable $newproducts. Below is my code. Thanks in advance
My Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\product;
class ProductsController extends Controller
{
public function index(){
$allproducts=product::all();
$bestsellerproducts=product::where('category','iphone 11')->get();
$newproducts=product::where('category','iphone 12')->get();
return view('test',['products'=>$allproducts],['bestsellerproducts'=>$bestsellerproducts],['newproducts'=>$newproducts]);
}
}
My view Template
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2>Best Seller products</h2>
@foreach($bestsellerproducts as $bestsellerproduct)
<label>Name</label>
{{ $bestsellerproduct['name'] }}
<label>Price</label>
{{ $bestsellerproduct['price'] }}
@endforeach<br>
<h2>New products</h2>
@foreach($newproducts as $newproduct)
<label>BaseName</label>
{{ $newproduct['name'] }}
<label>Price</label>
{{ $newproduct['price'] }}
@endforeach<br>
<h2>All products</h2>
@foreach($products as $product)
<label>BaseName</label>
{{ $product['name'] }}
<label>Price</label>
{{ $product['price'] }}
@endforeach<br>
</body>
</html>