55

I want to find out if a variable is a collection.

I can't use is_object() because it will be true even if it is not an collection. For now I use this, and it works:

if(is_object($images) && get_class($images) != 'Illuminate\Database\Eloquent\Collection') {

But I think it's so ugly that I spend time asking you about another solution.

Do you have any idea?

4

6 Answers 6

77

You can try something like this

if(is_a($images, 'Illuminate\Database\Eloquent\Collection')) {
    ....do whatever for a collection....
} else {
    ....do whatever for not a collection....
}

Or

if ($images instanceof \Illuminate\Database\Eloquent\Collection) {
    ....do whatever for a collection....
}
7
  • 6
    is_a has been deprecated since PHP 5.0, so while it's working, it's not the best advice.
    – Bogdan
    Jan 5, 2016 at 19:03
  • Strange. It still lists it in PHP 7 according to this: php.net/manual/en/function.is-a.php It does list it as deprecated. My bad.
    – P. Gearman
    Jan 5, 2016 at 19:04
  • 4
    @Bogdan is_a was deprecated in 5.0.0, but was reinstated (no longer deprecated) as of 5.3.0. However, given the history, I would still recommend instanceof over is_a. Plus, it reads a little better IMHO.
    – patricus
    Jan 5, 2016 at 19:08
  • 1
    Whoops, my bad :). Thanks @patricus.
    – Bogdan
    Jan 5, 2016 at 19:12
  • 3
    Note: I had to use \Illuminate\Database\Eloquent\Collection (with a leading back-slash)... May 21, 2018 at 14:36
51

The class being used is incorrect here. In a general sense, you should be testing for the base class.

use Illuminate\Support\Collection;

....
if($images instanceof Collection) { 
 ....
}
3
  • 1
    I have tried is_array, is_collection and others, but it seems only this way works the best!
    – Eddy Goh
    Jan 10, 2019 at 4:14
  • 1
    @EddyGoh there's no built-in PHP function called is_collection! Btw, Laravel collections look like arrays but they are not, so if you run is_array against a Laravel collection will always return false.
    – parse
    Jan 4, 2021 at 14:15
  • 1
    yes dear above is not working for me and this worked successfully. Aug 2, 2022 at 6:58
6
function is_collection($param): bool
{
    return (bool) (($param instanceof \Illuminate\Support\Collection) || ($param instanceof \Illuminate\Database\Eloquent\Collection));
}

If you are using it a lot you might just want to make function for it.

Combination of @Konchog & @Jonathan Ma answer.

2
  • \Illuminate\Support\Collection provides the 'contract' (interface) for all Collections - it's not really a base class. When we are coding, it's always best to use the most general solution when developing functions, because we can re-use the code in more places. Also, since Db\Elo\Coll. is a specialisation of Supp\Coll, your function is not really necessary. The point I am making is, if you need to test for Db (because your function/method depends upon a specialisation eg, loadMissingRelation(), then you should test for Db\Elo\Coll. Otherwise - just use the interface.
    – Konchog
    Oct 4, 2021 at 9:37
  • use Illuminate\Database\Eloquent\Collection as Collection; and then to check it's $thePossibleCollection instanceof Collection.
    – Andrew E
    Dec 1 at 6:16
5

The answer by @Konchong points this out, but I want to make a difference explicit for others.

There is a difference between Illuminate\Support\Collection, the base class used by many types of collections in Laravel, and Illuminate\Database\Eloquent\Collection, which extends the base class but has additional functionality and within its code, aliases the base class as BaseCollection. This is what you get when you execute a query builder, through a model class and a function like get(). An ordinary collection is made with collect(["my", "array"]) instead.

Note that both classes results in a different set of methods. For example, load, loadMissingRelation, and a number of other methods are present in the subclass that are not present in the parent class. Inside of the methods in the database subclass however, are a number of escape-hatch if statements that ensure that if someone accidentally uses the subclass when they mean the base class in an unintended fashion (such as adding an element to the collection), the collection will not break.

Note that the following is true:

User::get() instanceof \Illuminate\Database\Eloquent\Collection;
User::get() instanceof \Illuminate\Support\Collection;
collect(["hello", "i", "am", "a", "base", "collection"]) instanceof \Illuminate\Support\Collection;

Note that the following is false:

collect(["hello im a", "base collection"]) instanceof \Illuminate\Database\Eloquent\Collection;
is_array(collect(["hello im a", "base collection"]));

Be mindful that while a collection is essentially a wrapper around an array and with very powerful methods, it is not an array and can often fail or throw exceptions in the standard php array functions.

4

Just wanted to correct an error I ran into on this answer.

Note that instanceof excepts either a (obj) or the name of the class without quotes

$images instanceof Illuminate\Database\Eloquent\Collection

Also, interestingly enough there is a speed/performance difference using instanceof over is_a, but this is probably not relevant for you if you are like me and were searching for an answer to this question in the first place.

0
-3

For me using is_countable worked:

if(is_countable($somethingCountable)) {
// do something with array
} else {
// print something else

}

Read more about is_countable

2
  • 1
    is_countable() works with laravel Collection but it doesn't guarantee that the variable is a Collection instance. In fact if $somethingCountable is an array or another object that implements Countable it will return true. May 11, 2020 at 15:43
  • it's not the answer that the questioner is looking for. Aug 2, 2022 at 6:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.