mercredi 17 janvier 2018

How to get last 12 months user login data by per month in laravel

I am trying to get user login activity per month and then to show it in statistic in a bar chat. But the problem i am facing is that its not giving me any data. I will share my code first it will easier to explain my problem

My LoginActivity Model

class LogActivity extends Model
{
    protected $table = 'laravel_logger_activity';

    public function logActivity(){
        $videoPerMonth = array();

        for ($i=1; $i<=12; $i++){
            $age= 12 - $i;
            $userPerMonth[$i] = count(LogActivity::whereNotIn('userId', [1])->whereMonth('created_at', '=', date('n') -$age)->get());

        }
     }
    }

In this i do get user's activity per month but this is not accurate because being the first month if u subtract 12 value goes to negative. And i dont get actual reults. So, after reading few articles i changed my code to this

$userPerMonth =count( LogActivity::whereNotIn('userId', [1])->whereMonth('created_at', '>=', Carbon::now()->subMonth(12))->get());
return json_encode($userPerMonth);

But this returns empty.What shall i do ?

I want to get data by month vs activity For example nov 2017 : 300 , dec 2017:800,jan 01 2018:100 Something like that so i can put in bar chat Can anyone please help me with this Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mEMJOI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire