Delete in Laravel 5 MethodNotAllowedHttpException '

0

I'm trying to delete a post in Laravel and I get Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

So I have my delete button

<div class="col-md-6 text-right">
        <form method="DELETE" action="{{ url('news/news/destroy').'/'. $info->id }}" role="form">  
            {!! Form::submit('Delete this task?', ['class' => 'btn btn-danger']) !!}
        {!! Form::close() !!}
    </div>
</div>

MY controller

public function destroy($id)
    {
        $info = News::find($id);


        $info->delete();

        Session::flash('flash_message', 'Task successfully deleted!');

        return view('news.index',['info' => $info]);
    }

MY route

Route::delete('news/news/destroy/{id}', 'NewsController@destroy');

Is it the correct method?

    
asked by Bella 21.12.2017 в 22:07
source

2 answers

1

The form goes with POST and the DELETE is put in a hidden input whose name must be _method .

<div class="col-md-6 text-right">
    <form method="POST" action="{{ url('news/news/destroy').'/'. $info->id }}" role="form">
    <input type="hidden" name="_method" value="delete"/>
    {!! csrf_field() !!}
    {!! Form::submit('Delete this task?', ['class' => 'btn btn-danger']) !!}
    {!! Form::close() !!}
</div>
    
answered by 22.12.2017 в 08:28
0

you can also do the following

{{Form::Open(array('action'=>array('Controller@method',$info->id),'method'=>'delete'))}}
 {!! csrf_field() !!}
{{Form::close()}}
    
answered by 24.07.2018 в 19:12