Laravel delete user returns link with ID

Viewed 19

I am making a 1 page CRUD application in laravel, Whenever i delete a user i just edited, I get a blank page or a 404. I suppose this happens because the link it returns is: admin/user/{id}. I want to return the base url: admin/user Without the {id} at the end. Is there any way i can do this?

controller destroy code:

public function destroy($id)
    {
       $user = User::find($id);
       $user->delete();
       return back();
    }
1 Answers

Once the record is deleted, then you can't get that record.

If you want to return back then redirect on specific route.

public function destroy($id)
{
    $user = User::find($id);
    $user->delete();
    return redirect()->route('admin.user')->with('success', 'Your success message');
}

also, you can display the flash message when your URL redirect on page.

Happy Coding!!!

Related