Atmojo211
Atmojo211

Reputation: 27

Laravel undefine variable in view

I'm new to laravel. Using version 5.4 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: post" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.

My routes:

Route::get('test/{id}/edit','TestController@edit');

My controller:

public function edit($id)
{
    $post = Post::find($id);

    if(!$post)
        abort(404);
    return view('test/edit')->with('test', $post);
}

My form:

{{  Form::model($post, array('route' => array('test.update', $post->id), 'files' => true, 'method' => 'PUT')) }}

Upvotes: 0

Views: 48

Answers (2)

parker_codes
parker_codes

Reputation: 3397

Your controller is sending a variable named "test", but your error says that your blade file doesn't have the $post variable passed into it. This can be fixed by changing "test" to "post" in your controller.

Upvotes: 0

Andrew
Andrew

Reputation: 20091

You're assigning the post value to 'test', so should be accessible with $test rather than $post.

You probably want to do either of these two things instead:

return view('test/edit')->with('post', $post);

or

return view('test/edit', ['post' => $post]);

https://laravel.com/docs/5.4/views

Upvotes: 3

Related Questions