Hesham
Hesham

Reputation: 35

Missing required parameters for [Route: admin.destroy] on Laravel form action

I have this error:

Missing required parameters for [Route: admin.destroy] [URI: admin/{admin}]

That is the all view, and all variables, i tried a lot but i don't know what's wrong if i change put the second parameter $info this error appears The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.

    <div class="table-responsive">
        <table class=" table ">
            <thead class="thead-dark">
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Name</th>
                    <th scope="col">Code</th>

                    <th scope="col">Phone</th>
                    <th scope="col">Phone 2</th>
                    <th scope="col">Delete</th>
                    {{-- <th scope="col">email</th> --}}
                </tr>
            </thead>
            <tbody>
                @foreach ($infos as $info)
                <tr>
                    <td>{{ $info->id }}</td>
                    <td>{{ $info->name}}</td>
                    <td>{{ $info->code }}</td>
                    <td>{{ $info->phone }}</td>
                    <td>{{ $info->phone2 }}</td>
                    <td>
                        <button class="btn btn-danger btn-sm" onclick="handleDelete ({{ $info->id }})">Delete
                        </button>
                    </td>
                    {{-- <td>{{ $info->email }}</td> --}}
                </tr>

                @endforeach
            </tbody>
        </table>
    </div>

    <form action="{{ route('admin.destroy',['admin' => $info])}}" method="post" id="deleteInfoForm">
        @method('DELETE')
        @csrf
        <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModal"
            aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteModal">Delete Info</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <p class=" text-center text-bold">Are your sure ?</p>

                    </div>
                    <div class="modal-footer ">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">No , Go back</button>
                        <button type="submit" class="btn btn-danger">Yes , Delete</button>
                    </div>
                </div>
            </div>
        </div>
    </form>

this is my delete function from AdminController

public function destroy(Info $admin)
    {
        // $info = Info::find($id);

        $admin->delete();

        // session()->flash('succuss', 'Info deleted successfully');
        return redirect('/admin');
    }

my routing list

| DELETE    | admin/{admin}          | admin.destroy    | App\Http\Controllers\AdminController@destroy  

Upvotes: 1

Views: 1154

Answers (3)

Salim Djerbouh
Salim Djerbouh

Reputation: 11044

The route expects parameter 2 to be the model of the id used for route model binding

Add it to the action in the form

<form action="{{ route('admin.destroy', ['admin' => $info]) }}"

Update

The form is outside the foreach loop and therefor $info is undefined

Pass the form inside the foreach instead

<div class="table-responsive">
    <table class=" table ">
        <thead class="thead-dark">
            <tr>
                <th scope="col">#</th>
                <th scope="col">Name</th>
                <th scope="col">Code</th>

                <th scope="col">Phone</th>
                <th scope="col">Phone 2</th>
                <th scope="col">Delete</th>
                {{-- <th scope="col">email</th> --}}
            </tr>
        </thead>
        <tbody>
            @foreach ($infos as $info)
            <tr>
                <td>{{ $info->id }}</td>
                <td>{{ $info->name}}</td>
                <td>{{ $info->code }}</td>
                <td>{{ $info->phone }}</td>
                <td>{{ $info->phone2 }}</td>
                <td>
                    <button class="btn btn-danger btn-sm" onclick="handleDelete ({{ $info->id }})">Delete
                    </button>
                </td>
                {{-- <td>{{ $info->email }}</td> --}}
            </tr>
            <tr>
                <form action="{{ route('admin.destroy',['admin' => $info])}}" method="post" id="deleteInfoForm">
                    @method('DELETE')
                    @csrf
                    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModal"
                        aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="deleteModal">Delete Info</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <p class=" text-center text-bold">Are your sure ?</p>

                                </div>
                                <div class="modal-footer ">
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">No , Go
                                        back</button>
                                    <button type="submit" class="btn btn-danger">Yes , Delete</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>

Hope this helps

Upvotes: 1

Ayrton
Ayrton

Reputation: 2303

You're getting that error because your route expects to get an admin variable (admin/{admin}), but it isn't there when you call route in {{ route('admin.destroy')}}. You should provide it as the second parameter of the route method, in the keyed array format.

Upvotes: 0

zlatan
zlatan

Reputation: 3951

You're missing the data you want to delete in your form opening:

<form action="{{ route('admin.destroy', ['admin'=>$admin])}}" method="post" id="deleteInfoForm">

Before you do that, you have to pass the $admin variable to that view, in order to use it.

Upvotes: 0

Related Questions