How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web application is very simple to implement.

Find your registered user controller most likely at App\Http\Controllers\Auth\RegisteredUserController.php and in the create() method add the following code before the register page view is returned:

if (User::count() >= 1) {//Allow only 1 user to register
    return redirect()->route('home')->with('Registration not allowed currently');
}

This checks for if there is more than 1 user in the database to return to the ‘home’ route with the message ‘Only 1 user allowed’.

The performance impact of this is very minor even though a count is used the database either has 0 or 1 rows anyway making it a fast process.

You can obviously also adjust the number of users you want to be allowed registered.

Make sure if you are using an API to create users that you also apply this conditional to the store() method and return a response in JSON:

return Response::json(['message' => 'Registration not allowed currently']);