6 Replies - 425 Views - Last Post: 08 March 2019 - 08:22 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 215
  • Joined: 01-July 12

Laravel confirm delete in an alert in my view

Posted 06 March 2019 - 10:32 PM

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




Is This A Good Question/Topic? 0
  • +

Replies To: Laravel confirm delete in an alert in my view

#2 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,045
  • Joined: 12-December 12

Re: Laravel confirm delete in an alert in my view

Posted 07 March 2019 - 01:38 AM

What is your specific question?

You have not asked a question. I have pointed this out to you on previous occasions.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: Laravel confirm delete in an alert in my view

Posted 07 March 2019 - 05:51 AM

And why are you putting class functions in a view?
Was This Post Helpful? 0
  • +
  • -

#4 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 215
  • Joined: 01-July 12

Re: Laravel confirm delete in an alert in my view

Posted 07 March 2019 - 03:02 PM

I want to click a button in my view. (delete)
I want an alert or modal or some message to open up in
@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

I want the message in that section to confirm delete of a specific user by id
maybe I have to pass the id into a modal or session so I can call users.destroy if the alert shown confirm btn is clicked
Was This Post Helpful? -1
  • +
  • -

#5 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,045
  • Joined: 12-December 12

Re: Laravel confirm delete in an alert in my view

Posted 07 March 2019 - 10:45 PM

"I want" is not a question.

How To Ask Questions The Smart Way
Was This Post Helpful? 0
  • +
  • -

#6 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 215
  • Joined: 01-July 12

Re: Laravel confirm delete in an alert in my view

Posted 08 March 2019 - 06:27 PM

That was explaining the end answer of my problem...
Instead how a modal shows up on top of the page with a button click I wanted to know can i have it show up like an alert and I have my $user->id in a hidden text box so in the modal I have the id of the user I can pass to the confirm button and the confirm button can call the users.destroy

How can I have a modal be like an alert
and
How can I take the id in the modal and use it on a button to call destroy in my controller
 <div class="modal" id="mdelete" role="dialog" aria-labelledby="moddelete">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="moddelete">Confirm Delete</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">

                                    <p>Are you sure you want to delete</p>
                                </div>
                                <div class="modal-footer">
                                    <input type="hidden" name="txtid" id="txtid" />
                                    <input type="text" name="uid" id="uid" />
                                    
                                    <button type="button" class="btn btn-danger " data-dismiss="modal">No</button>
                                
                                    <span class="text-right">
                                        <button type="button" class="btn btn-primary btndelete">Yes</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
 @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-2">Show</a>
                                        <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white mx-2">Edit</a>
                                        <button type="button" class="btn btn-danger ml-2" data-toggle="modal" 
                                         data-target="#mdelete" data-id="{{$user->id}}" data-name="{{$user->username}}">Delete</button>
                                    </td>


Was This Post Helpful? 0
  • +
  • -

#7 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 215
  • Joined: 01-July 12

Re: Laravel confirm delete in an alert in my view

Posted 08 March 2019 - 08:22 PM

I have a modal with a hidden field of my current id that I need. I need to click a button in my modal to confirm if I should delete a user. I setup a form not sure if this is the best option and in javascript set the attr of form to the route but the route isnít finding the correct path although in the URL it says it correctly what am I missing maybe the route should be in php instead of JS? Thank you
<div class="modal" id="mdelete" role="dialog" aria-labelledby="moddelete">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="moddelete">Confirm Delete</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">

                                    <p>Are you sure you want to delete</p>
                                </div>
                                <div class="modal-footer">
                                    <form method="POST" id="formdelete">
                                        <input type="hidden" name="txtid" id="txtid" />
                                        <input type="text" name="uid" id="uid" />

                                        <button type="button" class="btn btn-danger " data-dismiss="modal">No</button>
                                    
                                        <span class="text-right">
                                            <button type="submit" class="btn btn-primary btndelete">Yes</button>
                                        </span>
                                    </form>
                                </div>
                            </div>
<a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-2">Show</a>
                                        <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white mx-2">Edit</a>
                                        <button type="button" class="btn btn-danger ml-2" data-toggle="modal" 
                                         data-target="#mdelete" data-id="{{$user->id}}" data-name="{{$user->username}}">Delete</button>
$(document).ready(function() {

    $('#mdelete').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var userid = button.data('id');
        var uname = button.data('name');
        var modal = $(this);
        modal.find('#txtid').val(userid);
        modal.find('#uid').val(userid);
        modal.find('.modal-body').text(
            'Are you sure you want to delete ' + uname);
    })

    $('#formdelete').submit(function() {
        var userid = $('#txtid').val();
        $('#formdelete').attr("action", "route('$users.destroy',$user->"+ userid +")");
        $('#formdelete').submit();
    });
});



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1