Undefined variable: data , $data is undefined. Laravel 8


I got an error says undefined variable data referring to my foreach in my blade

so my errors is :
Undefined variable: data (View: C:UsersadilaDesktoponlineshoponlineshopresourcesviewsuserproduct.blade.php)

so this is my blade file :

<div class="latest-products">
      <div class="container">
        <div class="row">
          <div class="col-md-12">
            <div class="section-heading">
              <h2>Latest Products</h2>
              <a href="products.html">view all products <i class="fa fa-angle-right"></i></a>
            </div>
          </div>`enter code here`
         
          @foreach($data as $product)


          <div class="col-md-4">
            <div class="product-item">
              <a href="#"><img src="assets/images/product_06.jpg" alt=""></a>
              <div class="down-content">
                <a href="#"><h4>{{$product->title}}</h4></a>
                <h6>$ {{$product->harga}}</h6>
                <p>{{$product->deskripsi}}</p>
                
              </div>
            </div>
          </div>

        @endforeach

        </div>
      </div>
    </div>

and this is my controller :

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use AppModelsUser;
use AppModelsProduct;

class HomeController extends Controller
{
    public function redirect(){
        $usertype = Auth::user()->usertype;

        if($usertype=='1'){
            return view('admin.home');
        }

        else{
            return view('user.home');
        }
    }

    public function index(){


        if(Auth::id()){
            return redirect('redirect');
        }
        else{

            $data = Product::all();

            return view('user.home',compact('data', $data));
        }
        
    }
}

and my Route :

Route::get('/', [HomeController::class, 'index']);

Please help me

Source: CSS – Stack Overflow

November 20, 2021
Category : News
Tags: css | laravel | Laravel 8 | laravel-blade | php

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.