Newbie
Newbie

Reputation: 1

How to apply toastr when user get leveled up?

This question is about when User get leveled up and toastr will pop up. This script is from my view blade template.

What I want to do is i want to put the toastr.js inside my script but I don't have idea how to implement.

** Profile.blade **

  <script type="text/javascript">
    $(document).ready(function(e){
     $.toast({
        text: 'You level up!',
        heading: 'Congratulations',
        icon: 'info',
        position: 'top-right',
        bgColor: '#d4af37',
        textColor: 'black'
    });
   })
  </script>

** Controller **

          while($user->curr_exp >= $user->exp_needed){
             if($user->level == 10 || $user->level == 15 || $user->level == 20)
               {
                  $user->reward_token += 1;
               }
                  $user->curr_exp -= $user->exp_needed;   
                  $user->prev_exp = $user->exp_needed;
               if($user->level <= 19){
                   $user->exp_needed = $user->prev_exp * 1.4;
               }
               elseif($user->level >= 20 && $user->level <= 39){
                   $user->exp_needed = $user->prev_exp * 1.1;
               }
               elseif($user->level >= 40 && $user->level <= 59){
                   $user->exp_needed = $user->prev_exp * 1.05;
               }
               elseif($user->level >= 60 && $user->level <= 79){
                   $user->exp_needed = $user->prev_exp * 1.04;
               }
               elseif($user->level >= 80 && $user->level <= 99){
                   $user->exp_needed = $user->prev_exp * 1.03;
               }
               elseif($user->level == 100){
                   $user->exp_needed = $user->exp_needed * 0;
               }
                   $user->level += 1;
                   $user->save();
               }

Any ideas how to work this? I'm using laravel framework 5.4.36 version on this one. I'm just confused how to display a live notifications on the user's profile every time they leveled up.

Upvotes: 0

Views: 1406

Answers (1)

Ducker
Ducker

Reputation: 198

This link should help you.

https://laramust.com/post/toastr-notification-in-laravel-application

There are two methods on the page, scroll halfway down the page to the 'You can use by Installing toastr package' part. I think that one will be most use to you.

Step 1: Install toastr package using composer.

$laravel_project> composer require yoeunes/toastr

Step 2: Include packages css and js file into layout.blade.php file or your view file where do you need to display toastr message.

@jquery - skip if already have.
@toastr_css - toastr css lib.
@toastr_js - toastr js lib.
@toastr_render - for render toastr notification

Step 3: Now add the service provider to config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.

'providers' => [
    ...
    Yoeunes\Toastr\ToastrServiceProvider::class
    ...
];

Step 4: Call toastr method into controller file.

toastr()->info('User has been created!')
//set message with title
toastr()->success('Have fun storming the castle!', 'Miracle Max Says')

Good luck!!

Upvotes: 1

Related Questions