0
votes

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
orang Indonesia ke ni?? by the way, instead of {{URL::to('coba/test/'.$post->id.'/edit') }}, kenape tak guna {{route('routename',["id", $post->id])}} .... sebelum tu, jangan lupa kasi name dulu pada route tadi Route::delete('test/{id}','TestController@destroy')->name('routename)`Syamsoul Azrien
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.Lars Mertens
Iya :) @SyamsoulAzrien .. sudah saya coba cuman masih sama error nyaAtmojo211
i learn from the video and i use the way on edit button then i will use it to delete/hapus too .. i think those are the same but i has message error .. im sorry im new for laravel. @LarsMertensAtmojo211
With my code below it will solve the MethodNotAllowedExceptionLars Mertens

1 Answers

0
votes

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.