Laravel Search Delete

Published 1 week ago by evripidesk

When I make search and I get the search results, I try to delete a record , its deleted show the message that successfully deleted but after I get the error throw new MethodNotAllowedHttpException($others);

​``` public function destroy($id) { $id=Hashids::decode($id); $id = implode(',', $id);

  $sub = Subs::find($id);
  $sub->delete();
  return response()->json(['success'=>"Subscription Plan Deleted 
  successfully.", 'tr'=>'tr_'.$id]);

} ​```

Call Function

`` a href="{{action('[email protected]', Hashids::encode($sub['id']))}}" class="btn btn-danger btn" data-tr="tr_{{$sub['id']}}" data-toggle="confirmation" data-btn-ok-label="Delete" data-btn-ok-icon="fa fa-remove" data-btn-ok-class="btn btn btn-danger" data-btn-cancel-label="Cancel" data-btn-cancel-icon="fa fa-chevron-circle-left" data-btn-cancel-class="btn btn btn-default" data-title="Are you sure you want to delete ?" data-placement="left" data-singleton="true"> Delete </a>

Routes ​```

 
Route::post('/subscriptions/search/results','[email protected]'); Route::get('/subscriptions/restore/{id}', '[email protected]');​
Best Answer (As Selected By evripidesk)
Snapey

The problem I think is returning back() If the url for the search was a post then when you do back a get is initiated and this will give an invalid method.

Ideally your search form should be a get

wilburpowery

You don't have the route pointing to the destroy method...

evripidesk

I have it Route::resource('subscriptions','SubsController');

Snapey
Snapey
1 week ago (960,065 XP)

Don't delete with a <a href= link. You need to post a form and that form needs to contain the delete hidden method field.

<form action="{{action('[email protected]', Hashids::encode($sub['id']))}}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit"><i class="fa fa-trash" /></i></button>
</form>
Kavyajain

Try this if using laravel backpack

In controller

$this->crud->addButtonFromModelFunction('line', 'deleteUser', 'deleteUser', 'end');

In Model

public function deleteUser(){
        return '<a data-button-type="delete" class="btn btn-danger actionButton" href="/admin/customers/'.$this->id.'" data-toggle="tooltip" title="DELETE"><i class="fa fa-trash"></i></a>';
    }

change href to the link where u want to delete.

Snapey
Snapey
1 week ago (960,065 XP)

Never delete with a link.... besides which you get Method not allowed when using resource controller because it does a GET on the delete route and there is not one.

evripidesk

@Snapey I try it but when I use my search form to find the subscription plan and I delete a record I get throw new MethodNotAllowedHttpException($others);

v0sn

Route

Route::delete('something/{id}', '[email protected]')->name('something.destroy');

you need that id here

Form

<form action="{{ route('something.destroy', $id) }}" method="POST">
    @method('DELETE')
    @csrf
    <button type="submit"></button>
</form>
evripidesk

@v0sn but the route delete already include to Route::resource('subscriptions','SubsController');

v0sn

@evripidesk yea thats right, then you just have to do the form right with the spoofed method and the csrf token & dont forget the $id

evripidesk

@v0sn all working fine, but when I use my search form to find a subscription and I press the delete then I get the error MethodNotAllowedHttpException

Saikishore

you can add one more route only for delete just before the resource route...as @v0sn mentioned above.

evripidesk

@Saikishore I get the same error

Snapey
Snapey
1 week ago (960,065 XP)

How many times... You MUST USE A FORM

evripidesk

@Snapey I am using the form but when redirect back after delete I get the above error, the form that I am using is the form that you post

v0sn

@evripidesk can you post your current code? Do you use a button inside the form? It won't work with an "a" element

Please sign in or create an account to participate in this conversation.