Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rachelmaeve's avatar

Run query on user login

Hello everyone at laracasts!

i'm quite new to laravel and i have a fairly simple question (I assume it is simple, could be wrong):

I have a column hasLoggedIn with a default value of TRUE and i would like to run a query as soon as the user is authenticated, so i can update my database without having to run this query on every page or every returned view.

Problem: I can't find where to put this query.

Any tip would be greatly appreciated! Much love from Germany and I hope you guys get to have a great weekend! :)

0 likes
4 replies
edoc's avatar

what larval version are you using?

Basically, you can write your logic to execute when users are authenticated in authenticated() method in LoginController if you are using 5.4

**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        // i use this to flash a message when users are logged in
        flash()->success("{$user->name}さんようこそ!!");
    }
1 like
rachelmaeve's avatar

I'm sorry, i forgot to specify that.

I'm currently running 5.2 on this project

// i cannot undo best answer :'( will update once solution found

DuikGek's avatar

I think if you make a general Class with static method and call the method in your controller when you need it, or if you gonna used more than once in one controller than you can use __construct.

rachelmaeve's avatar
rachelmaeve
OP
Best Answer
Level 1

I have found a solution to my problem: In vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

protected function handleUserWasAuthenticated(Request $request, $throttles)
    {
        if ($throttles) {
            $this->clearLoginAttempts($request);
        }
        DB::table('users')->where('email', $request->email)->update(FIELDS HERE);


        if (method_exists($this, 'authenticated')) {
            return $this->authenticated($request, Auth::guard($this->getGuard())->user());
        }

        return redirect()->intended($this->redirectPath());
    }

Please or to participate in this conversation.