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

thesimons's avatar

Showing content based on being auth / non auth

Hello,

I have ever worked with redirect (if not logged redirect to ...) but the project I'm working on requires a different approach. If logged show this content in the page, if not logged show that content in page.

My approach would be if auth load this view, if not auth load the other view.

Is there anything more elegant or more performant?

Thanks, Simon

0 likes
6 replies
Glukinho's avatar

My approach would be if auth load this view, if not auth load the other view.

This is fine too, you can do it in one line:

return view( Auth::check() ? 'dashboard' : 'welcome' );
martinbean's avatar

@thesimons Just use the built-in Blade directives to show content based on if the user is authenticated or not?

@auth
    <!-- Content for authenticated users -->
@else
    <!-- Content for guest users -->
@endauth

There’s no need to over-complicate the problem.

thesimons's avatar

@martinbean Thanks for your reply. This approach - in my opinion - is good when you want to show/hide some parts of the page, not completely load a different page.

Thanks a ton for the answer, I reply just to add my view and add content to this board :)

martinbean's avatar

This approach - in my opinion - is good when you want to show/hide some parts of the page, not completely load a different page.

@thesimons Then you can return a different view from your controller depending on if you have an authenticated user or not:

public function someAction(Request $request)
{
    // ...

    return view($request->user() ? 'auth-view' : 'guest-view');
}

Please or to participate in this conversation.