Laravel France

Undefined variable: products

Avatar de AlternantLaravel
AlternantLaravel

Bonjour, je suis en train de refaire le tuto de bestmomo, https://laravel.sillo.org/shopping-le-panier/ , mais sur laravel 8. j'ai créer la base de données et l'ai remplit. Mais quand je veux afficher la page panier, on me dit que la variable produit est indéfinit.

HomeController :

<?php
namespace App\Http\Controllers;
use App\Models\Product;

class HomeController extends Controller
{
    /**
     * Show home page
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products = Product::whereActive(true)->get();
        return view('dashbord', compact('products'));
    }
}

dashbord :

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Dashboard') }}
        </h2>
    </x-slot>

    <div class="container">
  
  <div class="row">
    <div class="col s12 cards-container">
      @foreach($products as $product)
        <div class="card">
          <div class="card-image">
            @if($product->quantity)
              <a href="#">
            @endif
              <img src="/images/thumbs/{{ $product->image }}">
            @if($product->quantity) </a> @endif
          </div>          
          <div class="card-content center-align">
            <p>{{ $product->name }}</p>
            @if($product->quantity)
              <p><strong>{{ number_format($product->price, 2, ',', ' ') }} € TTC</strong></p>
            @else
              <p class="red-text"><strong>Produit en rupture de stock</strong></p>
            @endif
          </div>
        </div>
      @endforeach
    </div>
  </div>
</div>
</x-app-layout>

web.php :

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
Route::get('/', function () {
    return view('welcome');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

Avatar de bestmomo
bestmomo

Salut,

Il y a bien des produits actifs dans la table ?

Vous ne pouvez pas répondre à ce sujet.