1

I tried to display all menus from the db in table form however i got an error saying that "undefined variable:menus(0)"

dashboard_index.blade.php

 <div class= "menu-content">
  <table class="table table-hover table-bordered">
    <thead>
      <tr>
        <th scope="col"></th>
        <th scope="col">Category Code</th>
        <th scope="col">Menu Title</th>
        <th scope="col">Menu Price</th>
      </tr>
    </thead>
    <tbody>
      @foreach ($menus as $menu)
      <tr>
        <td>{{ $menu->id }}</td>
        <td>{{ $menu->category_code }}</td>
        <td>{{ $menu->menu_title }}</td>
        <td>RM{{ $menu->menu_price }}</td>
      </tr>
      @endforeach
    </tbody>
  </table>
</div>

web.php

Route::get('/dashboard', 'AdminMenuController@index');

AdminMenuController.php

    public function index()
{
    $menus = \App\Menu::all();
    return view('admin.dashboard_index',      
    [
        'menus'=>$menus,
    ]);
}
2
  • 3
    Could you please post your code as text and not as image to ensure your question is still helpful even if the images are deleted? Commented May 25, 2021 at 10:31
  • Please post the code as text and not as pictures Commented May 25, 2021 at 10:34

1 Answer 1

2

You have a syntax error in $menu = \App\Menu::all()

it should be $menus = \App\Menu::all()

Sign up to request clarification or add additional context in comments.

7 Comments

That's not a syntax error, it's merely a typo. And we generally don't answer typo questions (because they hold no long term value, they only help the OP). We leave a comment under the question and flag the question as a typo (Flag > needs improvement > A community-specific reason > Not reproducible or was caused by a typo).
just realized, corrected it but i still got the same error
try @dd(menus) and tell me what comes up
it gives me an error "Use of undefined constant menus - assumed 'menus'"
@dd($menus); I'm sorry, I'm not using IDE here so you need to check the syntax after me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.