WallyJ
5 months ago
455
2
Laravel

Destroy route leads to Show route/view

Posted 5 months ago by WallyJ

This has got to be an easy fix but I can't see the tree for the forest. :)

I added a delete button to a list of contacts so each contact could be deleted. I Href'ed it to the contacts.destroy route, with an alert confirmation. When I click the button the confirmation window comes up. I click OK, and it shows me the contact view and does not delete the record. I am using resource routes for "contacts":

Controller @destroy:

public function destroy($id)
    {
        $contact = Contact::find($id);

        // Check for correct user
        if(auth()->user()->id !== $contact->user_id){
            return redirect('/contacts')->with('error', 'That Is Not Your Contact');
        }

        $contact->delete();
        return redirect('/contacts')->with('success', 'Contact Removed');
    }

Contacts View code for button:

<a class="btn btn-danger" onclick="return confirm('Are you sure you want to delete {{$contact->contact1firstname}} {{$contact->contact1lastname}}?')" href="{{route('contacts.destroy', $contact->id)}}">Delete</a>

Route as listed in the terminal:

|        | POST      | contacts                          | contacts.store        | App\Http\Controllers\[email protected]
               | web,auth     |
|        | GET|HEAD  | contacts                          | contacts.index        | App\Http\Controllers\[email protected]
               | web,auth     |
|        | GET|HEAD  | contacts/create                   | contacts.create       | App\Http\Controllers\[email protected]
               | web,auth     |
|        | DELETE    | contacts/{contact}                | contacts.destroy      | App\Http\Controllers\[email protected]
               | web,auth     |
|        | PUT|PATCH | contacts/{contact}                | contacts.update       | App\Http\Controllers\[email protected]
               | web,auth     |
|        | GET|HEAD  | contacts/{contact}                | contacts.show         | App\Http\Controllers\[email protected]

Why would it return my contacts.show view, showing the individual contact view?

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