leonnardo12's avatar

Undefined variable $products - foreach

Hi, how are you?! I am making an Ecommerce and my homepage "Welcome" should have a list of products, but I am not able to because it is giving undefined variable $products in foreach

what can I do?

its my foreach in welcome.blade.php

  @foreach($products as $product)
            <div class="card">
                <div class="imgBox">
                    <img src="" alt="" class="product">
                </div>
                <div class="contentBox">
                    <h3>{{$product['name']}}</h3>
                    <h2 class="price">R$ {{$product['price']}}</h2>
                    <a href="{{ url('#') }}" class="buy">Comprar</a>
                </div>
            </div>
            @endforeach

its my route.php

Route::get('/', [WelcomeController::class, 'index'])->name('welcome');

and its my Controller(WelcomeController.php)

<?php

namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;

class WelcomeController extends Controller
{
    public function index (Product $product){
        return view('welcome')->with(['products' => Product::all(), 'categories' => Category::all()]);
    }
}
0 likes
7 replies
Tray2's avatar

Why are you passing the product into you index method?

Try changing it to

 public function index ()
{
        return view('welcome')->with(['products' => Product::all(), 'categories' => Category::all()]);
}

If you still have issues try breaking the database queries out and see. if they work properly.

2 likes
leonnardo12's avatar

@Tray2 Thank you for responding. So, I have a second route and a second controller called "ProductController" which is in the admin panel and works by passing the product in the index method

public function index (Product $product){
        return view('product.index')->with(['products' => Product::all(), 'categories' => Category::all()]);
  }

and I can perfectly foreach into my product table, but only in Welcome.blade.php I am getting this error

Sinnbeck's avatar

Is that the complete view file? Does it work if you remove the foreach? Are you sure you are hitting the expected method (use dd() to check)?

2 likes
leonnardo12's avatar

@Sinnbeck

Here complete welcome.blade.php

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Index - ECOIN</title>
    <link rel="stylesheet" href="{{ asset('css/app.css') }}" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />

</head>
<body>
    <header>
        @include('layouts.navigation')
        <div class="main-video">
            <h1>ECOIN - SUA LOJA PREFERIDA GAMER</h1>
            <video src="images/valorant-video.mp4" id="vid" autoplay></video>
        </div>
    </header>
    <main>
        <h1>Resultado da Busca</h1>
        <section class="products">
            @foreach($products as $product)
            <div class="card">
                <div class="imgBox">
                    <img src=""
                        alt="" class="product">
                </div>
                <div class="contentBox">
                    <h3>{{$product['name']}}</h3>
                    <h2 class="price">R$ {{$product['price']}}</h2>
                    <a href="{{ url('#') }}" class="buy">Comprar</a>
                </div>
            </div>
            @endforeach
        </section>
    </main>
    <footer>
        <p>&copy; ECOIN - SENAC - 2022</p>
    </footer>
</body>

No, for some reason when I put dd($products) it is still undefined... I am new, sorry for some silly detail I missed

jlrdw's avatar

@leonnardo12 did you delete your first post? I had mentioned what to try to fix {{$product['name']}}.

2 likes
leonnardo12's avatar

@jlrdw Sorry, I had deleted the first post because I didn't quite know how to put the edited code here and I didn't see your answer. Ok, I put it exactly as you wrote there and it still gives error in foreach line

leonnardo12's avatar

Well, I managed to solve it by putting the route in the last line of the code... it was at the top, I don't know how this influences it, but it is solved

thanks for everyone!

Please or to participate in this conversation.