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

Twittler's avatar

Laravel Undefined variable:

Hi guys, just got a nooby problem, i cant fetch data from db, but the strange thing is in other folder i used

Controller:

    $hosting = hosting::all() -> toArray();
    return view("index",compact("hosting"));

View:

@foreach($hosting as $host) {{$host['pavadinimas']}} @endforeach

And that worked, right now i want to make a list and i using

Controller:

    $posting = posting::all() -> toArray();
    return view("lists.list",compact("posting"));

View:

@foreach($posting as $post)

<p>{{$post['vardas']}}</p>
@endforeach 

and it;s giving me error " Undefined variable: posting (View: C:\xampp\htdocs\praktika1\resources\views\lists\list.blade.php)"

and i don't understand what i have done wrong?

0 likes
11 replies
Vilfago's avatar

Are you sure you have data in $posting ?

Yorki's avatar

You should make some debugging

$posting = posting::all()->toArray();
dd($posting);

You could try then passing it without compact

$posting = posting::all()->toArray();

return view('lists.list', [
    'posting' => $posting,
]);

Also make sure you are actually calling proper action on your controller

Vilfago's avatar

For information, you don't need to transform the result to an array with toArray to loop your result. The collection instance is traversable.

You just have to write $host->pavadinimas instead of $host['pavadinimas']

ahmedggalal's avatar

you need to debug your code use dd() also , why are you calling ->toArray() , you can pass the collection

BishoyWagih's avatar

try this

in controller

$posting = posting::all();
 
return view("lists.list",compact("posting"));

in view

@foreach($posting as $post)
    <p>{{$post->vardas}}</p>
@endforeach 

if no results shown in the view,

so make sure $posting Collection is not empty, you can do this by

 $posting = posting::all();
     dd($posting)
Twittler's avatar

Thats even dummier question from me where i suppose to put this

$posting = posting::all(); dd($posting)

in controller like this?

    $posting = posting::all();
    dd($posting)
    return view("lists.list",compact("posting"));
BishoyWagih's avatar

just check $posting collection has data or not

in Controller

simply you can do

$posting = posting::all();
    return ($posting);

return view("lists.list",compact("posting"));

if no thing appears on the page then $posting collection has no data..

ahmedggalal's avatar

@Twittler yes like that

 $posting = posting::all();
    dd($posting)

dd = die and dump it will show you the content of the variable you are passing .

ahmedggalal's avatar

you need to verify if variable have data or not before passing it to the view , i think you can use

 if ( !empty ( $variable ) ) {
    //code
}
Twittler's avatar

Yeah guys thank you all very much, that was silly me, i forgot to change route to Route::get('/list', 'ListController@index');

Thank you all.

1 like

Please or to participate in this conversation.