jeudi 24 septembre 2015

Laravel 5 : Strange error, simple OR logic in middleware?

I just stumble upon on strange error simple OR logic in middleware.

public function handle($request, Closure $next)
{
    //can create user, 1 Super Administrator, 2 Administrator
    //dd(Auth::user()->role_id); //print 2

    if((Auth::user()->role_id != "1") || (Auth::user()->role_id != "2")){
        return Auth::user()->role_id . " is not equal with 1 or 2, logic true";
    }else{
        return Auth::user()->role_id . " is equal with 1 or 2, logic false";
    }

    return $next($request);
}

When i visit protected route, I always get 2 is not equal with 1 or 2, logic true, in this case Auth::user()->role_id is 2.

So is there anything wrong with my code ? but if i just run one statement, the logic is run very well, so I think the problem come, when I use more than one statement.

Actually i just want to check if logged in user role_id is 1 or 2 if so, then do next request, else return to somewhere with error message. But stumble upon on this weird error.

Thanks, any help appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire