Click here to Skip to main content
15,915,791 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This should be a simple task I am just not fully grasping laravel yet.
I have my controllers view and models setup. I want to use my users.destroy route to delete my row in the db. But I want to do it a certain way. I want to have an alert show In my alert area on my page asking to confirm the deletion of a certain user. Im assuming I need to pass the user id in a session to an alert to confirm my delete on a delete button click.
Click 1 button to open an alert on the top of my page if I click confirm it calls user.destroy.
PHP
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h4>View All Users</h4>

                    @if(session()->get('success'))
                        <div class="alert alert-success">
                            {{ session()->get('success') }}
                        </div>
                    @endif
                    @if(session()->get('danger'))
                        <div class="alert alert-danger">
                            {{ session()->get('danger') }}
                        </div>
                    @endif
                </div>
                <div class="card-body">
                    <div class="text-center my-2">
                        <a href="{{ route('register') }}" class="btn btn-primary">New User</a>
                    </div>
                    <div>
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Name</th>
                                    <th>Email</th>
                                    <th>Username</th>
                                    <th colspan="2">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($users as $user)
                                <tr>
                                    <th>{{$user->id}}</th>
                                    <td>{{$user->name}}</td>
                                    <td>{{$user->email}}</td>
                                    <td>{{$user->username}}</td>
                                    <td class="text-center">
                                        <a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a>
                                        <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a>
                                        <a href="#" class="btn btn-danger">Delete</a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
    public function destroy($id)
    {
        User::find($id)->delete();
        return redirect()->route('users.index')->with('success','User Deleted');
    }
Route::resource('users', 'UserController');


What I have tried:

using a session im assuming to bring my id $user->id and put that in my alert so i can call my users.destroy method in my controller to have a confirmation of deleting
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900