Support the ongoing development of →
Database Forms Laravel
Last updated by @tvbeek 1 year ago.

The syntax seems good but I would suggest you to use the compact method to send data to your view.

return view('home', compact('surveys'));

Thank you @faisal I tried that but it didn't seem to work either :) I'm still getting the Undefined variable $surveys error message.


Hello @shazam7734

The use for compact or not is a question of taste and in this case not part of your problem ;)

With $surveys = DB::select('select name, value from surveys'); you create a query builder but didn't execute it. I did check the documentation and see you miss the ->get() part.

I suspect if you update the code to $surveys = DB::select('select name, value from surveys')->get(); that you will receive the results.

ps. I have updated your post to make the code blocks more readable.


Hi @tvbeek

Thank you for your response and for updating my code blocks. I am new to this platform as well and wasn't sure how to do it.

I tried adding the ->get(); method to the end of my query builder, but am met with the same undefined variable error. I am not sure what I could be missing!


HI @shazam7734 ,

Strangely, you are still getting the error. I suggest you to please check the name of your view once again and also the variable name.

Try debugging the data stored in the variable before sending it to the view.

And finally, try to load the view without the foreach loop just to make sure that you are accessing the correct view.

Solution selected by @shazam7734

Hi @faisal

Thank you for your sound suggestions. I have managed to work out the issue :)


Sign in to participate in this thread!


Your banner here too?

Sarah shazam7734 Joined 25 Jul 2022


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.