Back

How to apply toastr notification when user get leveled up?


This question is about how to implement a live notification if the user gets leveled up. My leveling system consists of 100 levels, so every time a user levels up, a notification will pop up into their profile.

`` 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();
               }

``

Edit: I have already added a toaster to my profile.blade.php here is the script:

`` ** Profile.blade.php script **

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

``

I'm using laravel 5.4 on this one. I'm confused how to display a live notifications on the user's profile every time they leveled up.

Any ideas? Thank you

Luke Wright replied 3 months ago

Tremendous efforts go to evolve through to the notification salutes the way to define solutions definitely look forward to more levels updates or connect with official writers hub like custom dissertation writers packed knowledge deals.

Nine Hertz replied 1 month ago

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!!

Laravel Development Services


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us