Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an alert on Laravel 5?

I have a function where:
After I click the link named "hapus", the related data will be deleted and I want to have a popup alert to show that the data has been deleted.

*sorry for my bad english

*hapus technically means destroy

this the code:

public function hapus(Request $request, $id)
    {
        DB::table('kelompok')
        ->where('id', $id)
        ->delete();
        return redirect()->back();
    }
like image 406
Iqbal Nurkholik Avatar asked Aug 04 '16 10:08

Iqbal Nurkholik


1 Answers

Use with() in your controller

 function hapus(Request $request, $id)
        {
            DB::table('kelompok')
            ->where('id', $id)
            ->delete();
            return redirect()->back()->with('alert', 'Deleted!');
        }

In your blade template, retrieve the session after being redirected from controller:

@if (session('alert'))
    <div class="alert alert-success">
        {{ session('alert') }}
    </div>
@endif
like image 91
Peter Avatar answered Sep 29 '22 07:09

Peter