Categories: Development

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']);
Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago