简体   繁体   中英

Compare two objects in Laravel

I have two objects out of my User model in Laravel. Let's say

public function someFunction(User $user) {
    $adminUser = User::where('role', '=', 1);
    if($adminUser === $user) {
        return true;
    }
    return false;
}

Is this the proper way to compare two objects in Laravel? As per PHP Object Comparison , this should work.

Thanks for any input.

Since $user is a User instance, you can do this without executing any additional queries:

public function someFunction(User $user) {
    return $user->role === 1;
}

This code will return true if user's role is 1 and false if role is not 1.

You should try this :

public function someFunction(User $user) {

    if($user->role === 1) {
        return true;
    }
    return false;
}

hope this work for you !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM