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

thespice's avatar

Redirect to 404 from a blade view template

Hey guys,

I have a member profile system set up which uses the username as part of their profile page url.

Right now if any non-existing profile is called it throws non-object errors (as there is no user object being found to populate the data on the page).

To get around this I've tried using the following at the top of the profile template but it's not doing what I expected.

@php
  if (!$user) {
    return abort(404);
  }
@endphp

I was hoping that when an empty user object is found it would redirect to a 404 page, but this isn't working. Instead I'm getting error exception errors. Am I going about this the wrong way?

Would it be better to check the user object exists in the route instead, before serving the template?

0 likes
4 replies
wilburpowery's avatar

Why not do it from the controller? Why exactly would you do it from the view?

thespice's avatar

Doh... Too many late nights I guess... Thanks man ;)

Please or to participate in this conversation.