Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jestudillo's avatar

Undefined variable in Blade

Hola a tod@s: I have a problem, at this moment. I´m working with:

-PHP 8.1.6 (cli) (built: May 11 2022 08:55:59) (ZTS Visual C++ 2019 x64)

-Laravel Framework 9.45.0

But I have a problem during the presentation of the View. ¿Can you give me an opinion about the problem, please.? This is the message. https://shorturl.me/157If3n

Thank you so much.

0 likes
32 replies
tykus's avatar

I have the error in all migration file

There is an undefined variable in the view template; pass the data correctly to the view

jestudillo's avatar

@tykus sorry , but I don´t understand your answer. I´m a novice developer in Laravel. Can you explain me with a example.?

Regards

jestudillo's avatar

@tykus in my blade ( named "proveedor.blade.php" ) I´m not using a return function. Probably this is my mistake.

frankhosaka's avatar

Did you type localhost:8000/proveedor on the browser?

kokoshneta's avatar

Please don’t post error messages as screenshots – especially completely illegible ones hosted on sites with spam links.

Instead, copy the error message and paste it into your answer inside a code block. You make a code block by adding a line with three backticks (```) both before and after the code.

2 likes
kokoshneta's avatar

@jestudillo That’s still the same screenshot site (or whatever it is). I can’t even see the screenshot, just a mostly blank page with a link that goes to a phishing site. There’s no way to even tell what error you’re getting.

Post the actual error here, not a screenshot.

jestudillo's avatar

@kokoshneta the web browser sends me: -ErrorException -PHP 8.1.6 / 9.45.1 -Undefined variable $proveedores

Please check the following link: https://flareapp.io/share/4m4pDbZm#F55

But also in the (Collapse vendor frames) send me several issues. For example:

Illuminate \ Foundation \ Bootstrap \ HandleExceptions  : 45 handleError

Illuminate \ Foundation \ Bootstrap \ HandleExceptions  : 266 Illuminate\Foundation\Bootstrap{closure}

C:\xampp\htdocs\control_inventario\storage\framework\views\4c3977a812261a5fe93b1d45638d65ba4bb91b18.php  : 45 require

Illuminate \ Filesystem \ Filesystem : 109 Illuminate\Filesystem{closure}

kokoshneta's avatar

@jestudillo Ah, now this we can use!

The ‘several issues’ you see aren’t separate issues – that’s just the stack trace. It’s showing you every step of the code that led to the error, going backwards to the very beginning of the request.

The stack trace looks like you’re running with cached views, though, which isn’t a good idea when developing. Before you try anything else, do this and try reloading both the index route and the proveedor route:

php artisan optimize:clear

If that doesn’t solve it, please copy-paste the following code here (remember to put three backticks ``` on a separate line both before and after each block of code):

  • Your routes/web.php file, at least the part of it that defines the index route
  • The controller method that calls the view
mukeshchand's avatar

Did you pass the variable from the controller correctly? Check if there is a spelling mistake.

jestudillo's avatar

@mukeshchand in my Controller, i´m using:

public function index() { $proveedores = proveedores::orderBy('id','desc')->paginate(5); return view('proveedores.index', compact('proveedores')); }

but the error is the same.

== ErrorException == PHP 8.1.6 / 9.45.1 == Undefined variable $proveedores

Also in the "Collapse vendor frames" has more issues.

Thank you so much.

kokoshneta's avatar

@jestudillo Is your class called proveedores (plural, with a small p)? If so, why? It would be better to call it Proveedor (singular, capital P).

frankhosaka's avatar

Try this:

route::get('proveedor','MyController@proveedor'); in web.php

public function proveedor( ){$proveedores=proveedores::all();dd($proveedores);} in MyController

use localhost:8000/proveedor and tell us what you see.

tykus's avatar

@jestudillo but you see that you are doing nothing in the Closure to pass $proveedors to the view template, right?

Route::get('/index', function() {
    $proveedores = proveedores::orderBy('id','desc')->paginate(5);
    return View::make('pages.index', compact('proveedores'));
});

Make sure you understand how a Request is being handled so you are modifying the correct files

1 like
rhand's avatar

@jestudillo Well, where did you define the variable $Proveedor? Can you add code with code ticks here as suggested before? But the code for the controller used in the view? Two, why does the foreach use the same variable twice? Normally you use

@foreach ($proveedor as $proveedores)
{{ $proveedor->id }} {{ $proveedor->razon_s }} {{ $proveedor->rfc }} 
...
@endforeach
jestudillo's avatar

Hello amigos. At this moment I have a problem. I´m working with a dynamic droplist down (dependant) but Laravel sends me this error.

Illuminate \ Database \ QueryException PHP 8.1.6 9.45.1 SQLSTATE[42P01]: Undefined table: 7 ERROR: no existe la relación «pais» LINE 1: select "nombre", "id" from "pais" ^

I´m trying to implent the following funcionality in my form.

Droplist 1: Country (País) >> Droplist 2: State (Estado) >> Droplist 3: City (Ciudad)

But it is not working properly. Can you tell me the problem.? Can you help me, please.?

Thank you so much.

tykus's avatar

@jestudillo what does this mean to you?

no existe la relación «pais»

Run your migrations.

1 like

Please or to participate in this conversation.