Home » Php » php – Laravel 6: remove register route using User model in routes

php – Laravel 6: remove register route using User model in routes

Posted by: admin February 25, 2020 Leave a comment

Questions:

I would like to remove the register route from my Auth::routes() when a user with an admin role is registered without using App\User in my routes file.

use App\User;
if (User::where("role","=", "admin")->exists())
    Auth::routes(['register' => false, 'verify' => true]);
else
    Auth::routes(['verify' => true]);

Why would I want this?

This code causes a mysql error when i run php artisan without any tables in my database. I haven’t found any solution on how to solve this yet and it would be great if you can suggest me solutions on how to achieve this.

How to&Answers:

I suggest use Middleware .Use:

use Illuminate\Support\Facades\Route;

 public function handle($request, Closure $next)
 {
     $user=User::where("role","=", "admin")->first();
     $route = Route::getRoutes()->match($request);
     $currentroute = $route->getName();
     if($currentroute == "/register"){
       if(User::where("role","=", "admin")->count()>0){
       return $next($request);
        }
       else{
         abort(404)
        }
     }
}