Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

artisticre's avatar

Pulling first ID no matter what I am doing

I'm sure its a quick fix but I have been through this code trying things and I am not sure what is going on. On the destroy method, Its pulling the first ID in the table regardless of what I do.

blade view

route

Route::prefix('admin')->name('admin.')->group(function(){
    Route::post('/delete/{id}',[UsersController::class,'destroy'])->name('users.destroy');
});

controller

public function destroy(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $user->roles()->detach();
        $user->delete();        
        toastr()->success('User has been delete successfully');
        return redirect()->route('admin.users.index');
    }
0 likes
3 replies
Glukinho's avatar

I think you have messed up html code:

  1. <form> tags structure is wrong, opening and closing tags must be on the same level: https://prnt.sc/gxexpVpuk-d8

  2. you create separate form and modal for each $user, this seems wrong to me. Either there should be one modal with dynamic content, or at least give them distinct ids, now all modals have id="exampleModal" and all links call the same (first?) modal: <a ... data-bs-target="#exampleModal">...</a>

What is actually inside <form> tags? Can you give generated HTML from browser?

JussiMannisto's avatar

As @glukinho said, you're creating a separate modal for every user, which is weird. The id attribute is also the same for all modals, and a <div> in a <tbody> is not valid html.

tykus's avatar

You can vary the content in a single modal using JS; so like the others have already said, you can remove the modal markup from the table (and loop), and passing instead the URL for the destroy endpoint:

<script>
    $('#deleteUserModal').on('show.bs.modal', function (event) {
        var link = $(event.relatedTarget);
        var url = link.data('url');
        var modal = $(this)
        modal.find('form.deleteUserConfirmation').attr('action', url)
    })
</script>
1 like

Please or to participate in this conversation.