0
votes

I have condition in Controller with checks the value in database and based on this value is redirecting user to different login pages

public function getLogin() 
{               
    $login = DB::table('login')->pluck('login');
    //var_dump($login);
    if ($login == 1) {
        return View::make('users.login');
    } else { 
        return View::make('users.login1');  
    }        
}

When I go to login page I've got this error

Object of class Illuminate\Support\Collection could not be converted to int

When I var_dump($login); I get

object(Illuminate\Support\Collection)#304 (1) { ["items":protected]=> array(1) { [0]=> int(1) } }

How can I fix this error?

4
do you want to check if all the values in the collection is 1 or you want to check if it has at least 1 item in the collection?Oluwatobi Samuel Omisakin

4 Answers

1
votes

$login is a collection, you get all the values of table login with your query. if you want this create a for loop and have your if statement inside.

for example :

foreach ($login as $val) {
    if ($val== 1) {
         return View::make('users.login');
    } else { 
        return View::make('users.login1');  
    }    
}
1
votes

You can use it like this :

public function getLogin() 
{               
    $login = DB::table('login')->pluck('login');
    //var_dump($login);
    if ($login->count() == 1) {
        return View::make('users.login');
    } else { 
        return View::make('users.login1');  
    }        
}
0
votes

You should use isEmpty() or count() here, for example:

if (!$login->isEmpty())
if (count($login) > 0)
if ($login->count() > 0)
0
votes

Ok Its just simple in this case, You can use [0] with login to access it as int.

 public function getLogin() 
{               
    $login = DB::table('login')->pluck('login');
    //var_dump($login);
    if ($login[0] == 1) {
        return View::make('users.login');
    } else { 
        return View::make('users.login1');  
    }        
}