php - Laravel 5.1: How to set Route for update record -
i working laravel 5.1
i using routes of laravel.
i used form/html insert/update, stuck in routing of update record.
here route redirect edit page in routes.php
route::get('/company/edit/{id}','companymastercontroller@edit');
in companymastercontroller.php
public function edit($id) { $company = companymasters::find($id); return view('companymaster.edit', compact('company')); }
my action in edit.blade.php
{!! form::model($company,['method' => 'patch','action'=>['companymastercontroller@update','id'=>$company->id]]) !!}
and route action in routes.php
route::put('/company/update/{id}','companymastercontroller@update');
my controller action update.
public function update($id) { $bookupdate=request::all(); $book= companymasters::find($id); $book->update($bookupdate); return redirect('/company/index'); }
now when click on submit button gives me:
methodnotallowedhttpexception in routecollection.php
what doing wrong?
the main reason you're getting error because set form submit patch
method , you've set route put
method.
the 2 initial options have either have same method in route file form or set route to:
route::match(['put', 'patch'], '/company/update/{id}','companymastercontroller@update');
the above allow both methods used route.
alternatively, can use route:resource()
https://laravel.com/docs/5.2/controllers#restful-resource-controllers.
this take care of basic restful routes.
then take 1 step further can add following routes file:
route::model('company', 'app\companymasters'); //make sure namespace correct if you're not using standard `app\modelname`
then resource route like:
route::resource('company', 'companymastercontroller');
and in companymastercontroller
methods can type hinted e.g.
public function edit($id) {...}
would become:
public function edit(companymaster $company) { return view('companymaster.edit', compact('company')); }
obviously, don't have go approach though if don't want to.
hope helps!
Comments
Post a Comment