0

I am trying to delete an object without using forms. So what I did was improvising. I've made a route:

Route::get('category/destroyMe', ['uses' => 'CategoryController@destroyMe', 'as' => 'category.destroyMe']);

And in the view I am picking up the category id like so:

<a href="{{route('category.destroyMe', ['id' => $category->id])}}" ></a>

And finally the controller:

public function destroyMe()
{
    $this->destroy(Category::find(Input::get('id')));
    return redirect()->back();
}

Question: I was wondering if there is a way to do this without my helper method destroyMe()? Is there a way to forward a method = 'DELETE' via href or something, so i can call my resource route like category.destroy without the need of complication?

EDIT: I don't want to do it with forms (as I do know how to do it like that). Nor do I want to complicate with ajax or some extra scripts or something. I am wondering if there is a pure Laravel slick solution like

<a href="{{route('category.destroy', ['id' => $category->id, 'method' => 'DELETE])}}" ></a>
Norgul
  • 4,613
  • 13
  • 61
  • 144
  • Possible duplicate of [CRUD Laravel 4 how to link to destroy?](http://stackoverflow.com/questions/19643483/crud-laravel-4-how-to-link-to-destroy) – Fuzzyma Apr 17 '16 at 11:08
  • That duplicate doesn't answer my question – Norgul Apr 17 '16 at 11:35
  • It at least has plenty suggestions about how to do a delete call to the server. It seems that generating a form with the form controller is the most spreaded way of doing it – Fuzzyma Apr 17 '16 at 14:35
  • It does, but you are not looking that my method works. So I am not looking for a working solution, but specific one :) – Norgul Apr 17 '16 at 16:29
  • Well - then the simple answer is: no. There is no way to do a delete request with just a link. You always need a form or ajax. – Fuzzyma Apr 17 '16 at 16:55

2 Answers2

1

Try this:

<a href="{{ action('CategoryController@destroyMe', ['id' => $category->id]) }}" ></a>

And action:

public function destroyMe($id)
{
    Category::destroy($id);
    return redirect()->back();
}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I guess that will create the same route – Fuzzyma Apr 17 '16 at 11:09
  • I guess my code is the simpliest way to do what you want. – Alexey Mezenin Apr 17 '16 at 12:30
  • Yes, it is pretty slick, except that I need to modify the route itself to call 'destroyMe/{id}' or the code will break. However, I wanted to handle it using the resource destroy method withoud the need to create a new route at all – Norgul Apr 17 '16 at 16:36
0

I have a blog in which I can delete posts and this works for me

<a href="{{ route('delete-post', ['post_id' => $post->id]) }}">x</a>

The routing:

Route::get('delete/{post_id}', ['uses' => 'PostsController@delete', 'as' => 'delete-post']);

And the function in PostsController

public function delete($id) {
    Post::destroy($id);

    return redirect()->back()->with(["deleted" => "The post was deleted"]);
}
Angel Miladinov
  • 1,596
  • 4
  • 20
  • 43