Errorexception (E_ERROR) Undefined variable: messages

Asked

Viewed 724 times

-2

I am trying to display all records of a table but have the following error: Undefined variable: messages (View: /Users/Henrique/Documents/Laravel Projects/blog/Resources/views/messages.blade.php)

my controller:

public function getMessages() {
    $messages = \App\Message::all();

    return view('messages')->with(['Messages' => $messages]);
    //return view('messages',[ 'images' => $messages]);
}

my view:

@extends('layout.app')

@section('content')

    <h1>Messages</h1>

    @if(count($messages)>0)
        @foreach($messages as $message)
            <ul>
                <li class="list-group-item">Name: {{$message->name}}</li>
                <li class="list-group-item">Email: {{$message->email}}</li>
                <li class="list-group-item">Message: {{$message->message}}</li>
            </ul>

        @endforeach

    @endif

@endsection

@section('sidebar')

@endsection

1 answer

0


In the controller is 'Messages' uppercase! And in the view tries to take the lowercase variable in @foreach.

Modify in controller to:

return view('messages')->with(['messages' => $messages]);

You can do it like this.

return view('messages')->withMessages($messages);

Or:

return view('messages', compact('messages'));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.