thangphung
1 month ago
16
3
Vue

Count new users in daily, weekly, monthly using Laravel and Vue.js

Posted 1 month ago by thangphung

I want to show on the dashboard the numbers of users who signed up today, in a week, in a month. I've been able to print out the numbers but it needs a refresh to update. Is there anyway to update the stats without refreshing everytime?

Any help would be very appreciated. Thanks

This is my route:

Route::get('dashboard', '[email protected]');

My controller:

public function countuser()
    {
        $count = array("daily" => 0, "weekly" => 0, "monthly" => 0);
        $count['daily'] = User::where('created_at','>=',Carbon::today())->count();
        $count['weekly'] = User::where('created_at','>=',Carbon::today()->subDays(7))->count();
        $count['monthly'] = User::where('created_at','>=',Carbon::today()->subDays(30))->count();
        return response()->json($count);
    }

vue component:

<template>
  <div>

    <p>daily: {{ count['daily'] }}</p>
    <p>weekly: {{ count['weekly'] }}</p>
        <p>monthly: {{ count['monthly'] }}</p>

  </div>

</template>
<script>
export default {
    mounted(){
        
        this.count = this.countUser()

    },

     
    data(){
        return{
               count: ''
        }
    },

    methods: {

    countUser()
    {
      axios.get('/api/v1/dashboard')
        .then((response)=>{
          this.count = response.data
        });
    }

    },
}
</script>

Please sign in or create an account to participate in this conversation.