Skip to content
Advertisement

How to redirect Admin to a specific page and users to another – Laravel 8 jetstream

I’m trying to redirect admin to the admin panel page and the users to the homepage

i created new field for usertype and i add the value in database which is “admin”

JavaScript

then i created this Midlleware (AdminMiddleware):

JavaScript

then i add it to Kernel:

JavaScript

and i add the function to RouteServiceProvider and imported the class:

JavaScript

then i added the route to web.php:

JavaScript

and i created two accounts one of them is the admin with value of “admin” for usertype but when i login it still redirecting me to the homepage

what am i doing wrong here?

Advertisement

Answer

There is a simple way which I have been using in my app.

You don’t need a middleware for this purpose. Create a controller in the name of HomeController or whatever you want. In you LoginController change the property redirectTo to a simple route. For example ‘/home’ or even / for super simple routes. Now in routes/web.php add this route pointing to a method in HomeController.

JavaScript

Or

JavaScript

Additionally create your own routes which you want to redirect to.

JavaScript

 You can also create routes for number of users.

Now in your HomeController, in redirectUser() method, add your needs.

JavaScript

Please make sure that you have defined the same URI in your routes file.

You can also redirect users in your LoginController as well. But it works only after logging in. this method will also redirects user to the desired location when they click home button or the logo which contains homepage url.

Hope this one will help you.

Thank you.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement