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

ErikThiart's avatar

[Sanity Check] Eloquent Query on each login

Hello guys,

Will you please do a sanity check for me on the below approach.

In essence, I have a 3 step form, I want to direct the user back to the relevant step if they abandoned it and logged in again later.

Is there a way to see what this SQL query looks like in the backend, I like the simplicity, but I suspect this loads the entire business object

    /*
     * On each login check if a business profile is complete, if not redirect the user to the step to complete (will use a switch case)
     */
    if(!$user->business->website) {
        return redirect('/step-to-complete');
    }

What I actually just want is

SELECT column FROM table WHERE user_id = :user_id

That is a small and lightweight query to run when a user logs in, but I do not know if the laravel way actually ends up like that or if its loading everything (or worse, loading a collection of businesses into memory and then filter them)

Note: if the above is not information for you, let me know and I will elaborate as needed.

0 likes
5 replies
ollie_123's avatar

Hi Eric

If i understood correctly, you could do something like:-

public function index($id)
{
$user = \App\Business::where('user_id', $id)->first();
        if($user->website == '' || $user->mobile == '' || $user->name == '')
        {
            return redirect('/step-to-complete');
        }
        else 
        {
            return redirect('/dashboard');
        }
}
1 like
ErikThiart's avatar

Hey @ollie_123 I like that, I think I will probably go that route.

Curious, would the below be efficient?

    if( ! DB::table('businesses')->where('user_id', $user->id)->value('website') ) {
        return redirect('//step-to-complete');
    }
1 like
ollie_123's avatar

Hey @erikthiart

Nice! yeah thats a good approach & looks efficient as the ->value will pluck that specific column.

1 like
MichalOravec's avatar

@erikthiart You can add that logic to the authenticated method of LoginController

/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    if ( ! DB::table('businesses')->where('user_id', $user->id)->first()->website) {
        return redirect('//step-to-complete');
    }

    return $request->wantsJson()
                    ? new Response('', 204)
                    : redirect()->intended($this->redirectPath());
}
2 likes

Please or to participate in this conversation.