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

Rangana's avatar

Undefined variable: students (View: D:\exam\curd\resources\views\home.blade.php)

working with laravel 5.7 and need display student table data in the home page, home.blade.php

<table class="table">
                    <thered>
                     <tr>
                         <td>Id</td>
                         <td>Name</td>
                         <td>Address</td>
                         <td>Telephone</td>
                         <td>Actions</td>
                     </tr>
                    </thead>

                    <tbody>
                      @foreach ($students as $student)
                      <tr>
                        <td>{{$student->id}}</td>
                        <td>{{$student->name}}</td>
                        <td>{{$student->address}}</td>
                        <td>{{$student->telephone}}</td>
                        <td><a class="button is-outlined" href="">Edit</a></td>
                        <td><a class="button is-outlined" href="">Delete</a></td>

                      </tr>

                      @endforeach
                    </tbody>
                    </table>

StudentController.php

  public function index()
    {
        $students = Student::all();
        return view('home');
    }

web.php

Route::get('StudentController@index');

but got this error msg like this,

Undefined variable: students (View: D:\exam\curd\resources\views\home.blade.php)

how can fix this problem?

0 likes
4 replies
edoc's avatar

you forgot to pass it to the view @Rangana

public function index()
    {
        $students = Student::all();
        return view('home', compact('students'));
    }
luisrenelopez's avatar

  public function index()
    {
        $students = Student::all();
        return view('home', ['students' => $students] );
    }

Please or to participate in this conversation.