ErrorException (E_ERROR) Undefined variable: messages

-2

I'm trying to display all records in a table but it has 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
    
asked by anonymous 07.05.2018 / 03:58

1 answer

0

In the controller there are 'Messages' with a capital letter! And in the view try to get the variable with lowercase letter in @foreach.

Modify the controller to:

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

You can do this as well.

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

Or:

return view('messages', compact('messages'));
    
08.05.2018 / 14:53