To add SweetAlert Notifications inyour laravel Project

To add Tostr inyour laravel Project




1. Go tohttps://sweetalert.js.org/guides/ where you can see all details about toastr notifications.

3. Add the sweetalert in your project template. in my case it is the app.blade.php
add it in your header
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

4. You can now add notifications.
 add an extra script before your body end tag where you can put your condition

<script>
@if(Session::has('success'))
swal("{{ Session::get('success')}}");
@endif
</script>


In your laravel you can produce a session and trigger the toastr once there is a certain session you want.

Ex:
In my categories controller I made a session once a category was created
public function store(Request $request)
{
//
$this->validate($request,[
'name'=>'required'
]);
// dd($request);
$category = new Category;
$category->name = $request->name;
$category->save();

Session::flash('success', 'You successfully created a category');

return redirect()->route('categories');
}

Then in my app.blade.php I made a condition that will check if there is a success session then put the value in a sweetalert
<script>
@if(Session::has('success'))
toastr.success("{{ Session::get('success')}}");
@endif
</script>

Comments:

Post a Comment

Enter your comment...