Home > database >  large if else statement in php
large if else statement in php

Time:12-27

only fist two line execute in this code last two role not check how can solve this problem

$input = $request->all();
        $this->validate($request,[
            'email'=>'required|email',
            'password'=>'required'
        ]);
        if(auth()->attempt(['email'=>$input["email"], 'password'=>$input['password']]))
        {
            
            
            if(auth()->user()->role == 'accountadmin')
            {
                return redirect()->route('accountadmins');
            }

            else if(auth()->user()->role == 'maintainadmin')
            {
                return redirect()->route('maintainadmins');
            }

            if(auth()->user()->role == 'superadmin')
            {
                return redirect()->route('superadmins');
            }

            if(auth()->user()->role == 'subscriber')
            {
                return redirect()->route('subscriberss');
            }


            else
            {
                return redirect()->route('home');
            }
        }
        else
        {
            return redirect()
            ->route("login")
            ->with("error",'Incorrect email or password');
        }
if(auth()->user()->role == 'superadmin')
            {
                return redirect()->route('superadmins');
            }

            if(auth()->user()->role == 'subscriber')
            {
                return redirect()->route('subscriberss');
            }

without checking this role go next step. i have four login role using auth . but when i execute the only first two role check after that go next step . i try to else if else if but same problem

CodePudding user response:

You could use a switch control structure here. Like this:

switch (auth()->user()->role) {
    case 'accountadmin'  : return redirect()->route('accountadmins');
    case 'maintainadmin' : return redirect()->route('maintainadmins');
    case 'superadmin'    : return redirect()->route('superadmins');
    case 'subscriber'    : return redirect()->route('subscribers');
    default              : return redirect()->route('home');
}

CodePudding user response:

Although its much better to use middlewares or route -> permission binding, but to simply answer your question you can do the following:

$routes = [
    'accountadmins' => 'accountadmin',
    'maintainadmins' => 'maintainadmin',
    'superadmins' => 'superadmin',
    'subscribers' => 'subscriber'
];

Using array_search:

if (false !== $route = array_search(auth()->user()->role, $routes)) {
    return redirect()->route($route);
} else {
    return redirect()->route('home');
}

Personally I recommend to use a permission package such as spatie/laravel-permission it is very well maintained and documented:

  1. https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware#package-middleware

  2. https://github.com/spatie/laravel-permission

CodePudding user response:

superadmin, subscriber, systemadmin not login my route code Route::middleware(['auth','user-role:superadmin'])->group(function() { Route::get("/superadmin",[HomeController::class,'superadmin'])->name('superadmins'); });

Route::middleware(['auth','user-role:systemadmin'])->group(function() { Route::get("/systemadmin",[HomeController::class,'systemadmin'])->name('systemadmin'); });

Route::middleware(['auth','user-role:subscriber'])->group(function() { Route::get("/subscriber",[HomeController::class,'subscriber'])->name('subscriberss'); });

  • Related