0





183

Advertisement

I would like to count how many times a user has been logged in using the Events in Laravel.

I have this Event defined:

<?php namespace App\Handlers\Events;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use App\User;
use Illuminate\Support\Facades\Log;

class AuthLoginEventHandler {

    /**
     * Create the event handler.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  User $user
     * @param  $remember
     * @return void
     */
    public function handle(User $user, $remember)
    {
        Log::info('Logged in');

        $user->login_counter = 1;
        $user->save();
        $user->increment('login_counter');
    }

}

and in the EventServiceProvider I have the following:

<?php namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;

class EventServiceProvider extends ServiceProvider {

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'auth.login' => [
            'App\Handlers\Events\AuthLoginEventHandler'
        ]
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        //
    }

}

Update:

I have override the postLogin() from trait to the AuthController as:

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

    class AuthController extends Controller {

        use AuthenticatesAndRegistersUsers;

        /**
         * Create a new authentication controller instance.
         *
         * @param  \Illuminate\Contracts\Auth\Guard  $auth
         * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
         * @return void
         */
        public function __construct(Guard $auth, Registrar $registrar)
        {
            $this->auth = $auth;
            $this->registrar = $registrar;

            $this->middleware('guest', ['except' => 'getLogout']);
        }


        public function postLogin(Request $request)
        {
            $this->validate($request, [
                'email' => 'required|email', 'password' => 'required',
            ]);

            $credentials = $request->only('email', 'password');

            if ($this->auth->attempt($credentials, $request->has('remember')))
            {
                Log::info('Logged in User from AuthController');
                return redirect()->intended($this->redirectPath());
            }

            return redirect($this->loginPath())
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'email' => $this->getFailedLoginMessage(),
                ]);
        }
    }

This is not working on my Laravel 5.0, I dont know why, is there any other alternative to count the logins of user other than using Events?

I am checking the logs and there is nothing comming in, when I try Log::info('Logged in'); within the boot method, it comes to logs.

Question author Artan | Source

Advertisement


0


First thing, because you have added something up into Provider, you need to clear the previous compiled source (which is cached to speed up loading time).

$ php artisan clear-compiled

Secondly, your code has counting problem,

    $user->login_counter = 1;  <-- set count to 1 ??
    $user->save(); <--- then SAVE?
    $user->increment('login_counter'); <--- INCREMENT for what?

Instead, it should be like this

    $user->login_counter += 1;
    $user->save();

Make sure in the migration you have the default (initial) value for login_counter

$table->integer('login_counter')->unsigned()->default(0);

Then it just works!

Answer author Pete-houston