Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raoufkeskes's avatar

redirect user after Log in to different pages according to its type LARAVEL 5.4

i ve tried this method on my LoginController and it did not work it still redirect me to my user.home View that i ve deleted . obviously i got this error InvalidArgumentException in FileViewFinder.php line 137: View [user.home] not found.

<?php 
public function authenticated(Request $request, $user)
    {
         $type = $user->userable_type ;
        switch ($type)
        {
            case 'Student':
                return redirect()->intended('/Student/Home');
                break;
            case 'Teacher':
                return redirect()->intended('/Teacher/Home');
                break;
            case 'Company':
                return redirect()->intended('/Company/Home');
                break;
        }
    }

0 likes
7 replies
Snapey's avatar

i don't think its this code, rather middleware is hijacking the request.

a common culprit is the redirectIfAuthenticated middleware. in there you will see /home. change that to some random string and see if you get sent there.

we can then work out why this is getting involved

1 like
raoufkeskes's avatar

Hello i ve fixed it by modifying App/Http/Middleware/RedirectifUser.php

public function handle($request, Closure $next, $guard = 'user')
    {
        if (Auth::guard($guard)->check()) {
            
             $type = Auth::guard($guard)->user()-> userable_type ;
            // dd($type) ;
             switch ($type)
            {
                case 'Student':
                    return redirect('/Student/Home');
                    break;
                case 'Teacher':
                    return redirect('/Teacher/Home');
                    break;
                case 'Company':
                    return redirect('/Company/Home');
                    break;
            }
        }

        return $next($request);
    }

i ve tried to redefine my function in my LoginController.php but it didn t work so i ve kept it in the Middleware Php file

Snapey's avatar

Bad!

You will have problems with this approach. Surely every request will be redirected?

jlrdw's avatar

At very least add a default:

1 like
raoufkeskes's avatar

Snapey Yes Every request actually it s redirected I know that it s a Bad approach but i did not find any solution else ! jlrdw yes i ve added a default File & add some Comments for Reusable Code !

if someone can help me to not edit the middleware File & everything work as I want it s gonna Be awesome

Snapey's avatar

you were following the right approach in the first place. I suggested a way to identify that it was the guest middleware causing the issue, but I cannot see that you tried it?

Please or to participate in this conversation.