Support the ongoing development of Laravel.io →
Database Forms Laravel

Hello! I am new to laravel and am using version 5.8. I have created a Likert survey where users rank various points and have succeeded in saving the results in a MySql database. I am now trying to pull their results from the database and display it on their profile page. I have created a $surveys variable in my SurveyController.php file that are holding the DB select items, made a route for it in my web.php file, and have tried calling the variable in the home.blade.php file. However, I keep getting the variable as being undefined. Here is my files with codework so far:

SurveyController.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Survey;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SurveyController extends Controller
{
public function index()
    {
        $surveys = DB::select('select name, value from surveys');
        return view('home',['surveys'=>$surveys]);
    }
}

web.php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SurveyController;

Route::get('Results',['SurveyController@index']);

home.blade.php

        <thead>
            <tr>
                <td>Teaching Point</td>
                <td>Survey Result</td>
            </tr>
        <thead>
        <tbody>
            @foreach ($surveys as $survey)
            <tr>
                <td>{{ $survey->name }}</td>
                <td>{{ $survey->value }</td>
            </tr>
            @endforeach
        </tbody>
    </table>

The error comes within the "foreach" loop saying the "surveys" variable is undefined. I have defined it in my SurveyController.php file, so any help as to what I'm missing would be greatly appreciated!

Last updated by @tvbeek 2 years ago.
0

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'));
0

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

0
moderator

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.

0

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!

0
Solution

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.

0
Solution selected by @shazam7734

Hi @faisal

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

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Sarah shazam7734 Joined 25 Jul 2022

Moderators

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

Your logo here?

Laravel.io

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

© 2025 Laravel.io - All rights reserved.