Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.5K
Views
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:\Users\adila\Desktop\onlineshop\onlineshop\resources\views\user\product.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 App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Product;

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Please remove $data from compact() function

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

You passed the variable ‍$data‍ to the ‍‍‍‍view('user.home', compact('data')) but you have an error in the product.blade I think the problem is on the product. blade file. check that file

over 4 years ago · Santiago Trujillo Report

0

You error is in the product.blade.php view and in your controller you're returning the home.blade.php view.

You can avoid the Undefined variable error in your product.blade.php adding an isset in your view like this :

@if(isset($data))
   @foreach($data as $product)
       (...)
   @endforeach
@endif
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!