Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.5K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda