0

i need to specify redirection depent on which user is authentified. I add a column in the default laravel users table : "is_admin" with values "1" and "0"

In my controller i tried :

 if(Auth::user()->is_admin=1){
  return view('index');
 }
 else {
 return view('auth.login'); 
  };

But seem's my condition always true even if the auth has is_admin =0

How can i solve that problem ? Is there a better way to do this condition ? thanks

3
  • what is "is_admin" ? Commented Sep 22, 2018 at 21:59
  • a new column i added in the table users Commented Sep 22, 2018 at 22:01
  • what is the type of the column in which you save the is_admin Commented Sep 22, 2018 at 22:15

2 Answers 2

2

Your problem is that you're assigning a value in your if statement, you've got a single = which assigns a value, rather than a double == (or triple ===) to compare values.

What's happening when you've only got a single = is that you're assigning the value 1 to whatever is before the = and that will evaluate to a "truthy" value meaning that the if will always be true no matter what and the else will never execute.

You should change it to this:

if (Auth::user()->is_admin == 1){ // Notice the two == here
    return view('index');
} else {
    return view('auth.login'); 
}

Notice that I have removed the semicolon at the end, it isn't necessary.

Sign up to request clarification or add additional context in comments.

2 Comments

In order to avoid mistakes like this in the future, you should utilize Attributes. In your User model, create an attribute called "getIsAdminAttribute" and let it return true or false. Then you would just reference it like this: if(Auth::user()->isAdmin). By doing it this way, you eliminate the potential error of assigning values when you meant to evaluate.
Or can keep constants on the left side of check. Like if (1 == Auth::user()->is_admin) {/**/} or if (true == $unknownState) {/**/} If ever there is forgotten one equal sign and stays only one equal sign between, it will throw an error. +1 for spotting error.
1

To avoid this kind of issue you kind manage the way you specify value by creating const in you User Model. this allow you to have the same values which have same type all the time

class User extends Model
{
    const ADMIN_USER = "1";
    const REGULAR_USER = "0";

    public function is_admin()
    {
        return $this->attributes['is_admin'] == self::ADMIN_USER;
    }
}

when creating a non admin user you use this

User::create([
    // SET OTHER ATTRIBUTE
    'is_admin' => User::REGULAR_USER
]);

when creating the admin user you use this

User::create([
    // SET OTHER ATTRIBUTE
    'is_admin' => User::ADMIN_USER
]);

And when it come to check it in your view, you will have just to call the is_admin method on the Auth::user()->is_admin()

if(Auth::user()->is_admin()){
    // Your code goes here
} else {
    // And other codes goes here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.