Laravel 4 - Login count using standard authentication

591 Views Asked by At

I was wondering if someone could point me in the right direction.

I have just started using laravel 4 and i want to add a login count to my application.

Im using the standard authentication from the laravel docs.

Im guess that i have to add something like the following SQL ... But do i add it from the routes file or the User.php model ... and then how would i call it from the login route?

$sql = "UPDATE some_table SET total_logins = total_logins + 1 WHERE user_id = $userID";

Any help would be awesome

1

There are 1 best solutions below

1
Needpoule On BEST ANSWER

First, instead of sql, i would use Eloquent.

Then, you can access your logged in user with the Auth::user() function, and use Eloquent increment function :

Auth::user()->increment('total_logins');
Auth::user()->save();

I would do that in your login function in your UserController file.

if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
    Auth::user()->increment('total_logins');
    Auth::user()->save();
}