user2301515
user2301515

Reputation: 5107

Laravel delete with confirm

I try to delete with confirm like, using a controller method delete:

function delete($id) { 
    $list = Todolist::find($id);
    return view('lists.delete')->with('list',$list);
}

and corresponding delete.blade.php:

{!! Form::open(array('route' => array('lists.destroy', $list->id), 'method' => 'delete', 'class' => 'form')) !!}
<button type="submit" class="btn btn-sucess">Delete</button>
<button type="submit" onClick="history.back()">Cancel</button>
{!! Form::close() !!}

then also a controller destroy-method

function destroy($id) {
    $list = new Todolist;
    //$list->delete($id);
    echo 
    return view('lists.confirmdelete')\Redirect::route('lists.index')  
            ->with('message', 'Task deleted!');
        ////how to aply 5 second sleep for showing message 'Task deleted!'???
}

and confirmdelete.blade.php

<h1>{{ $list->name }}</h1>
<p>{{ $list->description }}</p>
<p><b>{{ $message }}</b></p>

How to do, that it display "Task deleted!" messsage e.g 5 seconds and then two steps back to an index action?

Upvotes: 1

Views: 6373

Answers (3)

GNZL
GNZL

Reputation: 11

Easier with:

<button class="btn btn-link" 
        onclick="return confirm('Are you sure you want to delete the record {{ $user->id }} ?')">
   DELETE
</button>

In this case, you open a popup with a confirmation if you want to delete a record before clicking. Obiously you need to put

$user = User::find($id);

in the delete method if you want an id in the message.

Upvotes: 1

Ajarudin Gunga
Ajarudin Gunga

Reputation: 453

session variable best for given problem try in controller method

function destroy($id) {   

    //AFTER SUCCESS YOUR LOGIC
    Session::flash('message', 'Status Changed');
    Session::flash('alert-class', 'alert-success');
    return redirect('/index');
}

in blade file you can check like this demo

@if(Session::has('message'))
<div class="alert {{ Session::get('alert-class') }} fade-in" id="alert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{{ Session::get('message') }}
</div>
@endif

Session flash is best to use here it will poof once it print. and you can use JS for after 5 second fadeout.

<script>
    $("#alert").fadeTo(2000, 500).slideUp(500, function(){
    $("#alert").slideUp(500);
});
</script>

Upvotes: 0

ARIF MAHMUD RANA
ARIF MAHMUD RANA

Reputation: 5166

In your destroy method you have to do a little tweek & also have to use a little bit of js

Change destroy method this to

function destroy($id) {
    $list = new Todolist;
    $data = [
      'name' => $list->name,
      'description' => $list->description,
    ];
    $list->delete($id);
    $data['message'] = 'Task deleted!';
    $data['redirectRoute'] = route('lists.index');

    return view('lists.confirmdelete', $data);
}

and in confirmdelete.blade.php

<h1>{{ $name }}</h1>
<p>{{ $description }}</p>
<p><b>{{ $message }}</b></p>
<script>setTimeout(function(){ window.location.href = '{{ $redirectRoute }}' }, 5000);</script>

Upvotes: 2

Related Questions