سؤال

I'm building a blog with laravel and have edit functionality and resourceful controllers.

Here is the edit method from my controller :

public function edit($id)
{
    $post = Post::find($id);
    return View::make('posts.edit')->with('post',$post);
}

And my edit view :

<div class="containeredit">
{{Form::open(['action'=>'PostsController@update'])}}
{{Form::label('Title: ')}}
{{Form::text('title',$post['title'])}}
{{Form::label('Body: ')}}
{{Form::textarea('body',$post['body'])}}
<br>
{{Form::submit('Add Blog Post',['class'=>'btn btn-primary'])}}
{{Form::close()}}

and at last this is my update controller method

    public function update($id)
{
    $post = Post::find($id);
    $input = Input::all();
    $post->title = $input->title;
    $post->body = $input->body;
    $post->save();
    return "hi";
}

But when I click on the edit button , I'm being routed to this url :

http://localhost:8000/posts/%7Bposts%7D

which leads to a not found error , How do I fix this?

هل كانت مفيدة؟

المحلول

The update route expects an id, so you'd change

{{Form::open(['action'=>'PostsController@update'])}}

to

{{Form::open(['action' => ['PostsController@update', $post['id']], 'method' => 'put'])}}

More info about the routes it creates can be found here: http://laravel.com/docs/controllers#resource-controllers

نصائح أخرى

You want to update existing row so you probably want to let laravel know the id of that row :)

{{Form::open(['action'=>['PostsController@update', $post->id], 'method' => 'patch'])}}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top