I have a problem where I need a product list that is stored in a database to be displayed on my start.blade.php
page, but an error shows:
Undefined variable $products
Here is all of my code:
start.blade.php:
@extends('layouts.app')
@section('content')
<section class="product-section">
<div class="product-list">
<h1>Product List</h1>
@forelse ($products as $product)
<div class="product">
<p>{{ $product->name }}</p>
<p>{{ $product->weight }}</p>
<p>{{ $product->type }}</p>
<p>{{ $product->price }}</p>
<p>{{ $product->supplier }}</p>
<p>{{ $product->extra }}</p>
<p>{{ $product->amount }}</p>
</div>
@empty
<p>No products found.</p>
@endforelse
</div>
</section>
@endsection
web.php:
use AppHttpControllersProductController;
Route::get('/start', [ProductController::class, 'index'])->name('logged.start');
ProductController.php:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsProduct;
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
// Pass the products to the 'logged.start' view
return view('logged.start', compact('products'));
}
}
Product.php:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Product extends Model
{
use HasFactory;
protected $fillable = ['name', 'weight', 'type', 'price', 'supplier', 'extra', 'amount'];
}
Can someone please help see the fix that neither I nor the robot can’t find?
I have tried everything from checking if the routes are correct, to making a new controller, but nothing is making that list appear from the database!
2
Answers
In your controller, you’re returning the view as logged.start, but your start.blade.php file is likely in the resources/views folder.
The error Undefined variable $products usually occurs when the variable $products isn’t passed to the view as expected. Here are some potential reasons and solutions for this issue:
Check Your Route and View Name
The route in your web.php file points to the ProductController@index method and specifies the name of the view:
Route::get(‘/start’, [ProductController::class, ‘index’])->name(‘logged.start’);
However, in your ProductController, the view you’re returning is named ‘logged.start’:
If the start.blade.php file is actually located in the resources/views directory instead of resources/views/logged/, update the view path in ProductController to match the correct path:
Verify Database Connection and Data Availability
Ensure the database connection is set up correctly in your .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Then, check if there is data in the products table in the database. You can check by running a query in your database client or using tinker:
This will return all products from the products table. If it returns an empty collection, you may need to seed or insert data into the products table.
Check if Your Product Model is Configured Correctly
Your Product model appears correct, but it’s worth double-checking that it matches the products table schema. Also, confirm that the table name matches Laravel’s plural convention or, if not, explicitly specify it in the model:
protected $table = 'products';
// Only needed if your table name doesn’t follow Laravel’s conventionClear Cache (Optional)
If the issue persists after trying the above steps, Laravel’s cache might be causing the problem. Clear the application cache:
php artisan config:cache
php artisan route:cache
php artisan view:clear
php artisan cache:clear
Final ProductController.php Code
After ensuring all the above, your ProductController should look like this:
After implementing these steps, check if the $products variable is successfully passed to the start.blade.php view. This should resolve the Undefined variable $products issue.