Gatzmar
Gatzmar

Reputation: 143

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'));
    }
}

Upvotes: 0

Views: 72

Answers (4)

Cups
Cups

Reputation: 6896

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.

Upvotes: 0

Rama Durai
Rama Durai

Reputation: 750

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.

Upvotes: 0

Sebastian
Sebastian

Reputation: 1001

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

Upvotes: 1

Franz Gleichmann
Franz Gleichmann

Reputation: 3579

$user_rolesis 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

Upvotes: 2

Related Questions