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

shaddark's avatar

Laravel 9 - register form condition

Hello, i was trying to create a condition for display the Register button form in my app. I need to show the register button only first time when there are not registered users in db, and make disappear it if user with id 1 will be registered. (basically because the user 1 will be admin, and for next users the admin will see the Register route for create new users and roles)

How could i do it?

                        @guest
                            @if (Route::has('login'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                                </li>
                            @endif
                                
                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                                </li>
                            @endif
                        @else

0 likes
5 replies
MohamedTammam's avatar
Level 51
@if (Route::has('register') && ! User::exists())
<li class="nav-item">
	<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
@endif

But I recommend doing that logic using env variables. No need to run a query every time the page refresh just to see whether there's a user in the database or not.

1 like
tykus's avatar

Basically, you want something like this (better to move the Eloquent query into the Controller)

@if (Route::has('register') && App\Models\User::count() === 0)
    <li class="nav-item">
        <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
    </li>
@endif

After that, you want a separate register route behind the auth middleware so you can (i) be authenticated as and (ii) are authorized to create users.

1 like
shaddark's avatar

@tykus @mohamedtammam thanks both for the reply, i mixed the two answers by doing like this:

          @if (Route::has('register') && !App\Models\User::exists())
               <li class="nav-item">
                      <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
               </li>
           @endif	

I mixed because mohamed's answer missed the App\Models Yes this is working, and now i'm working on middleware, but i didn't really understand how to do like @tykus said. I can't find register middleware in Middleware/Kernel.php

UPD: Never mind i did it :D

Please or to participate in this conversation.