Jump to content
exceedinglife

laravel confirm delete in an alert in my view

Recommended Posts

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.

	<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');
	 
	

Share this post


Link to post
Share on other sites

Present the alert with Javascript so that you don't have to do multiple requests. That means embedding or otherwise constructing the "alert" HTML beforehand, then showing and/or hiding as necessary according to user action.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.