php - How to update exsiting row in database in laravel5 -
i new in laravel , want update existing row in database,but when click on send button in view (for example 127.0.0.1/laravel/public/article/update/3 ) encounter following error:
methodnotallowedhttpexception in routecollection.php line 201:
here code
route route::get('article/edit/{id}','articlecontroller@edit'); route::get('article/update/{id}','articlecontroller@update');
articlecontroller public function edit($id) { $change = article::find($id); return view('edit',compact('change')); } public function update($id, request $request) { article::update($request->all()); return redirect('article'); }
model public $table = 'article'; protected $fillable = ['title' , 'body'];
edit.blade.php <h1>ویرایش بست {{$change->title}}</h1> {!! form::model($change ,['method'=>'patch' , 'url'=>['article/update' , $change->id ]]) !!} {!! form::label('title','عنوان') !!} {!! form::text('title') !!} <br> {!! form::label('body','متن') !!} {!! form::textarea('body') !!} <br> {!! form::submit('send') !!} {!! form::close() !!} @if($errors->any()) <ul class ='alert alert-danger'> @foreach($errors->all() $error) <li>{{ $error }}</li> @endforeach </ul> @endif
the easiest way resolve routing issues laravel use 'artisan'.
http://laravel.com/docs/5.1/artisan
if use command:
php artisan route:list
you'll see every possible route , http verb available use. error in routecollection can fix these issues looking @ app/http/routes.php
file.
you defined route follows:
route::get('article/update/{id}','articlecontroller@update');
then call route via form follows:
{!! form::model($change ,['method'=>'patch' , 'url'=>['article/update' , $change->id ]]) !!}
your routes.php
definition not match form's patch method, you're getting method not allowed exception because patch route not defined.
you need line of code in routes.php
file:
route::patch('article/update/{id}','articlecontroller@update');
i highly recommend using instead of defining each method individually:
route::resource('article', 'articlecontroller');
then run following command again artisan see routes created:
php artisan route:list
Comments
Post a Comment