0

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"));
}
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
Longha Seng
  • 1
  • 1
  • 5

1 Answers1

3

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

Community
  • 1
  • 1
mrQubeMaster
  • 342
  • 2
  • 14
  • or `@foreach ($displays as $display)` should be `@foreach ($display as $display)` – devpro Feb 07 '19 at 08:42
  • Thank you for your answer. I already added 's' to 'display' but it still alerts the same message: "ErrorException (E_ERROR) Undefined variable" – Longha Seng Feb 07 '19 at 08:43
  • try to `dd($displays);` in your controller to see if it has data. if it has there, try it in your blade. looks like it may be empty. make sure you have a check for that in your blade – mrQubeMaster Feb 07 '19 at 08:47
  • also, ` $displays = Info::where('id', 1)->get();` could be replaced with `$displays = Info::find(1);` [this](https://laravel.com/docs/5.7/eloquent#retrieving-single-models) makes in smaller and better looking – mrQubeMaster Feb 07 '19 at 08:49
  • 1
    @devpro That would still break as $display is not set. Also you should not use the same variable twice in a foreach really, it can get confusing and will probably break. – Kyle Wardle Feb 07 '19 at 12:25