Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Form model binding and resource controller error

I'm building a really simple CRUD in laravel just to learn something about this framework. It works all like a charm but I can't make the update function of a controller work properly.

Here my situation:

1) I build a resource controller using artisan command.

2) I build a form view using blade and I Open the form with this code:

<!-- Form -->
@if($mode=="edit")
    {{ Form::model($task, array('route'=>array('task.update',$task->id),'files'=>true)) }}
@else
    {{ Form::open(array('route'=>'task.store','files'=>true)) }}
@endif

It works great and every field are filled with the right data. The generate url of the form's action is:

http://localhost/mysite/task/2

The problem is that when I submit this form I get this error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

Someone can understand why? Can I help you with more information?

like image 627
MatterGoal Avatar asked Jul 03 '13 09:07

MatterGoal


1 Answers

You need 'method' => 'put'.

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

As you can see here.

http://laravel.com/docs/controllers#resource-controllers

Verb:     PUT/PATCH
Path:     /resource/{id}
action:   update
route:    resource.update

EDIT: To trigger the update()-action you must send a PUT or PATCH-request to the route resource.update, in your case task.update.

like image 136
edenstrom Avatar answered Oct 31 '22 14:10

edenstrom