Question

I'm testing to see if a user already has a record (d) in the database.

My query:

$userResults = Results::where('d','=',Input::get('d'))
                      ->where('user','=',Input::get('user'),'AND')->get();

with var_dump returns

object(Illuminate\Database\Eloquent\Collection)#333 (1) { ["items":protected]=> array(0) { } }

when empty or an array of results if there.

How do I test the object for isset?

Was it helpful?

Solution

You just use count to check the result:

// Illuminate\Database\Eloquent\Collection implements Countable interface
if (count($userResults) === 0) {
  // empty result
}

or just use the isEmpty method:

if ($userResults->isEmpty()) {
  // empty result
}

OTHER TIPS

Use Collection method for this:

$userResults->isEmpty(); // true / false

Or count since Collection implements Countable interface

count($userResults); // 0 if empty

if (empty($userResults)) { // do the job }
if(isset($userResults)){
   //you criteria
}

OR

if(empty($userResults)){
       //you criteria
    }
   if($userResults->count()){
     //your code
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top