SohailAQ
SohailAQ

Reputation: 1028

Laravel - ErrorException Undefined variable

I have tried to refer all the possible solutions in stackoverflow but could not solve this.

This is my Controller.

public function getMessages(){
    $messages = Message::all()->toArray();
    return View('admin.thesis', ['messages' => $messages]);
}

This is my route/web.php

Route::get ('/thesis','MessagesController@getMessages');

And this is my view

<div class="panel-heading">Thesis Details</div>
    <div class="panel-body">
        <table class="table table-bordered">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>E-Mail</th>
                <th>Contact</th>
                <th>State</th>
                <th>Country</th>
                <th>Article</th>
                <th>Author</th>
                <th>Uploaded File</th>
            </tr>
            @foreach($messages as $row)
            <tr>
                <td>{{$row['firstName']}}</td>
                <td>{{$row['lastName']}}</td>
                <td>{{$row['email']}}</td>
                <td>{{$row['contactNumber']}}</td>
                <td>{{$row['state']}}</td>
                <td>{{$row['country']}}</td>
                <td>{{$row['article']}}</td>
                <td>{{$row['author']}}</td>
                <td>
                    <a href="{{ URL::to('/') }}/uploads/{{$row['file_path']}}">
                      {{$row['file_path']}}
                    </a>
                </td>
            </tr>
            @endforeach
        </table>
    </div>
</div>

This is my error

ErrorException Undefined variable: messages (View: C:\xampp\htdocs\fileupload\resources\views\admin\thesis.blade.php)

How do I resolve this?

Upvotes: 1

Views: 5710

Answers (3)

Sapna Bhayal
Sapna Bhayal

Reputation: 802

I used following code and its work for me :

class SidebarController extends Controller
{
    public function news_widget() {

        $posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
        return view('index', array('data'=>$posts));
    }
}

And within the view I simply use $data and its working fine for me.

Upvotes: 0

Dexter Bengil
Dexter Bengil

Reputation: 6625

Try this on your Controller

public function getMessages(){
    $messages = Message::all();
    return view('admin.thesis')->with(['messages' => $messages]);
}

And in your blade file

@foreach($messages as $row)
<tr>
    <td>{{ $row->firstName }}</td>
    <td>{{ $row->lastName }}</td>
    <td>{{ $row->email }}</td>
    <!-- etc... -->
    <td>
       <a href="{{ URL::to('/') }}/uploads/{{ $row->file_path }}">
         {{ $row->file_path }}
       </a>
    </td>
</tr>
@endforeach

Upvotes: 3

Sand Of Vega
Sand Of Vega

Reputation: 2416

Try this on your Controller

public function getMessages(){
    $messages = Message::all()->toArray();
    return view('admin.thesis', compact('messages'));
}

Upvotes: 0

Related Questions