4

I am working with laravel framework project and facing below issue.

Query:

$query = DB::table('test');
$query->select('*');
$query->where('testId = 1');
$result = $query->get();
print_r($result);

Output :

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
        )

)

Now I am checking $result have record or not.

if(empty($result))
{
   echo "Not Empty check with empty()";
}

if(count($result) == 0)
{
   echo "Not Empty check with count()";
}

Output:

Not Empty check with count()

Question :

I have used empty() in all the projects but in laravel framework project I am not able to know that why this $result going in count() condition and not going in empty().

Note:

I have read that count() is slow compare to empty() also empty() check variable is set or not so I am using empty() in all return array or object array.

Please help someone.

Thanks in advance!

4
  • your query is returning the result and not empty so count is always greater than zero, check with !empty($result) condition Commented Jul 19, 2017 at 8:08
  • But in above case why its going to count() and not going in empty(). This array returns no value from database so !empty() will gives true which is not right. @RAUSHANKUMAR Commented Jul 19, 2017 at 8:11
  • as the first() query returns a collection object so better to go with isEmpty() function Commented Jul 19, 2017 at 8:16
  • Yes collection object working with count() inbuilt method of php and isEmpty() is laravel method so ofcourse it will work. Thanks for your advice ! Commented Jul 19, 2017 at 8:32

4 Answers 4

5

If you use collection, you sould use isEmpty() method

docs: https://laravel.com/docs/5.4/collections#method-isempty

Sign up to request clarification or add additional context in comments.

4 Comments

Yes correct, So I am able to use count() or isEmpty().. right?
yep both are ok :)
Yes, isEmpty() is laravel method and count() php common method. Thanks for your help !
there is a method ->count() for collection too. no problem
2

With ->get() method, you will get an instance of collection.

When you apply empty() on data obtained from get method, you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results.

When you obtain data with ->get() method, you can't use following simply,

if(empty($data)){ 

}

if(!data){ 

}

if($data){ 

}

Instead of above you can simply use,

isEmpty($data) or 

count($data) or 

$data->count()

4 Comments

Thanks for your advice !
I think you shouldn't have problem with first() since it will return null if no data is found. I wonder why
I have edit question with get()... Thanks for correction and your answer works and helps lot !
@RJParikh Congrats, Glad it helped you :D
1

From the php docs

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

The laravel collection class implements the Countable interface which lets you use the count function. If you check the collection class you'll see that the count method returns the count of the internal items.

There is no interface for the empty function so in this case it only checks if the variable is not set or equals to false which obvious doesn't apply in case an object is checked.

1 Comment

Ok got it... Thanks for your help :)
0

Condition Try with this:

if(empty($result->toArray()))
{
    echo "Not Empty check with empty()";
}

if(count($result) == 0)
{
   echo "Not Empty check with count()";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.