问题
I'm an amateur in laravel. I use laravel 5.4. so I want to make process delete without form binding but I have an error message like this. Please tell me how to solving this.
route:
Route::delete('test/{id}','TestController@destroy');
My Form:
<td><button type="button" class="btn"><a href="{{URL::to('coba/test/'.$post->id.'/edit') }}" >Edit</a></button><button type="button" class="btn"><a href="{{ action('TestController@destroy', $post['id']) }}" method="post" >Hapus</a></button>{{ csrf_field() }}{{ method_field('DELETE') }}
</td>
My Controller:
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect()->to('coba/test');`
}
回答1:
Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
Delete
{{ Form::open(['url' => 'test/'.$post->id, 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
Edit
{{ Form::open(['url' => 'coba/test/'.$post->id.'/edit', 'method' => 'POST']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }}
only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding id of the post so you dont have multiple html forms in your code.
来源:https://stackoverflow.com/questions/46753514/laravel-5-4-methodnotallowedhttpexception-in-routecollection-php-line-251