what is the right route for destroy method?

  laravel, model-view-controller, php

I’m making a website using laravel 7.30.4, in this website there is a page called Hobbies where all hobbies are listed and next to every hobby there are options to see the details of the hobby, to edit or delete the hobby.
delete button is supposed to call destroy method in the controller and details button is supposed to call show method in the controller.
whenever I click on delete button it takes me to details view which is called by show method in the controller, so instead of calling the destroy method destroy button is calling the show method.I’m pretty sure that I put the right routes for both methods within the buttons.

this is the index.blade.php file where the view of Hobbies and the buttons code are:

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">All Hobbies</div>

                    <div class="card-body">
                        <ul class="list-group">
                            @foreach($hobbies as $hobby)
                                <li class ="list-group-item">{{$hobby->name}}<a class="ml-2" href="/hobby/{{ $hobby->id }}">details</a>
                                    <a class = "ml-2 btn btn-sm btn-outline-primary" href="/hobby/{{ $hobby->id }}/edit"><i class="fas fa-edit"></i>edit</a>
                                    <form style="display: inline;" action="/hobby/{{$hobby->id}}" methode="post">
                                        @csrf
                                        @method('DELETE');
                                        <input class="btn btn-outline-danger btn-sm ml-2" type="submit" value="delete">
                                    </form>
                                </li>

                            @endforeach
                        </ul>
                        <a class="btn btn-success btn-sm mt-3" href="hobby/create"><i class="fas fa-plus-circle"></i>create new hobby</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

and here are the two methods in my Controller file

public function show(Hobby $hobby)
    {

        return view('hobby.show')->with('hobby',$hobby);
    }
public function destroy(Hobby $hobby)
    {

        $old_name=$hobby->name;
        $hobby->delete();
        return $this->index()->with([
            'meldg_success' =>' Hobby<b> '. $old_name. '</b> Hobby deleted'
        ]);
    }

in cmd, the routes list for hobbycontroller shows:
the URI for hobby.show is hobby/{hobby} with method GET|HEAD and for
hobby.destroy is hobby/{hobby} with method DELETE

I dont know why its being mixed between the routes of show and destroy methods I am very beginner in Laravel.

Source: Laravel

Leave a Reply