Search code examples
formslaravel-5.7laravelcollective

Laravel collective custom forms, action Controller@update not defined


This is my route:

Route::get('admin/edit-news/{id}', 'AdminNewsController@edit');

My Controller@update method:

 public function update(Request $request, $id)
    {
        $news = News::find($id);

        $news->title = $request->input('title');
        $news->content = $request->input('content');

        $news->save();

        return redirect ('/admin');
    }

and my view with custom form:

{{ Form::open(['action' => ['AdminNewsController@update', $news->id], 'method' => 'POST']) }}   
                {{ Form::bsText('title', $news->title) }}
                {{ Form::bsTextArea('content', $news->content) }}
                {{ Form::hidden('_method', 'PUT') }}            
                {{ Form::bsSubmit('Confirm', ['class' => 'btn btn-primary center-block']) }}
            {!! Form::close() !!}

The error im getting is

"Action App\Http\Controllers\AdminNewsController@update not defined. (View: D:\xampp\htdocs\laravelhotel\resources\views\admin\news\edit_news.blade.php)"

I dont know why, since the action i put is update function, and i have all the components registered in FormServiceProvider.


Solution

  • If you use PUT method, which is simulated by POST form-method and _method field ({{ Form::hidden('_method', 'PUT') }}), you need to use the corresponding route:

    Route::put('admin/edit-news/submit', 'AdminNewsController@update'); 
    //     ^^^