To add SweetAlert Notifications inyour laravel Project

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>

How to add Toastr notifications in your laravel Project

How to add Toastr notifications in your laravel Project

To add Tostr inyour laravel Project

1. Go to https://github.com/CodeSeven/toastr where you can see all details about toastr notifications.
 
2. Save each JS and CSS
            Save them in laravel's  public folder, in js and css

3. Add the  toastr in your project template. in my case it is the app.blade.php
At your header
<link rel="stylesheet" href="{{ asset('css/toastr.min.css')}}">

Add Jquery library below your css
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Before the end tag of your body
<script src="{{ asset('js/toastr.min.js')}}"></script>
4. You can now add toast notifications.
 add an extra script below the import of tostr.min.js

<script src="{{ asset('js/toastr.min.js')}}"></script>

<script>
toastr.info('Are you the 6 fingered man?')
</script>

For more example how to use it, refer to this link and demo
https://codeseven.github.io/toastr/demo.html

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 toastr
<script>
@if(Session::has('success'))
toastr.success("{{ Session::get('success')}}");
@endif
</script>