Albert Albert - 1 year ago 88
PHP Question

Laravel: find out if variable is collection

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?

Answer Source

Couldn't you use

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


if($images instanceof 'Illuminate\Database\Eloquent\Collection') {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download