Search code examples
phplaravelundefined-variable

Undefined Variable in Laravel PHP


I'm new to Laravel and I'm very struggling with it. Please help me. I want to retrieve data from database to display on web page. But It alert message:

ErrorException (E_ERROR) Undefined variable

display.blade.php

@foreach ($displays as $display)
     {{ $display->first_name }}
     {{ $display->last_name }}
@endforeach

DisplayController.php

public function index(){                                              
    $displays = Info::where('id', 1)->get();    
    return view('display', compact("display"));
}

Solution

  • you have a typo in your displaycontroller.php

    return view('display', compact("display"));
    

    should be

    return view('display', compact("displays"));
    

    you are missing the s behind display