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']);
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Some early September spring shots from a bushwalk, It is still too early for wildflowers.
I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…
If you ever needed a simple solution in Laravel to delete an entire directory and…
React icons is the easiest way to get all the common and popular icon packs…