简体   繁体   中英

If statement never evaluates to true (even when it should)

I'd created an if statement "if ($user_roles == 3) " and this $user_roles has a value of "3" the condition is supposed to be true but the result is always false.

here is my code below:

public function ViewSponsorInfo($sponsor_id)
{
    $id = $sponsor_id;
    $user_id = User::where('id','=',$id)->get();
    $user_roles = [];
    foreach ($user_id as $id) {
        array_push($user_roles, $id->role);
    }/*
    dd($user_roles);*/

    if ($user_roles == 3) {
        $orga = Organization::where('orga_id','=',$sponsor_id)->get();
        dd($orga);
        return view('pages.Ngo.View-Sponsor-Information',compact('orga'));
    }else{
        $indi = Individual::where('indi_id','=',$sponsor_id)->get();
        dd($indi);
        return view('pages.Ngo.View-Sponsor-Information',compact('indi'));
    }
}

$user_roles is not 3 and can never be. it's an array. its contents, however, can be three.

try:

if(in_array(3, $user_roles)) { ...}

for reference: in_array

$user_roles is an array. So your if statement is always false.

I think, try this one.

foreach ($user_id as $id) {
    array_push($user_roles, array('role' => $id->role));
}/*
dd($user_roles);*/

foreach ($user_roles as $user_role) {
if ($user_role['role'] == 3) {
    $orga = Organization::where('orga_id','=',$sponsor_id)->get();
    dd($orga);
    return view('pages.Ngo.View-Sponsor-Information',compact('orga'));
}else{
    $indi = Individual::where('indi_id','=',$sponsor_id)->get();
    dd($indi);
    return view('pages.Ngo.View-Sponsor-Information',compact('indi'));
}
}

or

foreach ($user_id as $id) {
    array_push($user_roles, array('role' => $id->role));
}/*
dd($user_roles);*/

foreach ($user_roles as $user_role) {
if ($user_role->role == 3) {
    $orga = Organization::where('orga_id','=',$sponsor_id)->get();
    dd($orga);
    return view('pages.Ngo.View-Sponsor-Information',compact('orga'));
}else{
    $indi = Individual::where('indi_id','=',$sponsor_id)->get();
    dd($indi);
    return view('pages.Ngo.View-Sponsor-Information',compact('indi'));
}
}     

Please said something upon it after tried this one.

Judging from what you show your function ViewSponsorInfo() by its very name is only interested in detecting if this user has the sole role '3' in your db.

I mean the outcome is binary isn't it? Either they are a 3 or they are not, no need to go looping thru results.

$user_id = User::where('id','=',$id)->get();
if($user_id[0] !== 3 ) {  //***
  doSponsorLink();
}else{
  doNonSponsorLink();
}

I'm not familiar with Laravel or the db layer your are using, so maybe that is even simply if($user_id) .

untested, and here I am assuming user can only have 1 single role, and we do not seem to be making allowances for the user not existing in the db.

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