Laravel: find out if variable is collection

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Determining if a Variable is an Eloquent Collection in Laravel Introduction Laravel, being one of the leading PHP frameworks, provides an impressive collection of tools and features to make our life easier. One such feature is the Eloquent ORM which offers a convenient way to manage database interactions. However, sometimes, we need to identify if a variable is indeed an instance of `Illuminate\Database\Eloquent\Collection`. This post will demonstrate various ways to achieve this goal while also providing useful insights into Laravel's collection handling. 1. The "Ugly" Solution (Code Block 1) The most straightforward method to determine if a variable is an instance of `Illuminate\Database\Eloquent\Collection` could be the one you mentioned, which uses two conditionals:
if(is_object($images) && get_class($images) != 'Illuminate\Database\Eloquent\Collection') {
}
This solution works but comes with a few drawbacks. First, it's not very elegant and can be improved by introducing more concise conditionals. Additionally, this approach might not catch all possible cases of non-collection instances that are still defined as objects (such as custom classes). It does, however, provide a starting point for exploring different techniques. 2. The "Better" Solution (Code Block 2) We can refine the previous solution by checking if the variable is an instance of `Illuminate\Database\Eloquent\Collection` directly using the instanceof keyword:
if(is_object($images) && $images instanceof 'Illuminate\Database\Eloquent\Collection') {
}
This solution is much more accurate and elegant, but it assumes that only collections can be instances of this class. If you have a custom object that contains a collection as one of its properties or another variable that might be an instance of `Illuminate\Database\Eloquent\Collection`, this approach would still lead to incorrect results. 3. The "Best" Solution (Code Block 3) The most robust and reliable way to determine if a variable is an instance of `Illuminate\Database\Eloquent\Collection` in Laravel involves using the `is()` function from the Type Determination facade:
use Illuminate\Support\Facades\Type;
if(Type::is($images, 'Illuminate\Database\Eloquent\Collection')) {
}
This solution is both easy to understand and highly accurate. It takes advantage of Laravel's extensive type-discovery capabilities to check if the given variable adheres to the specified class or interface definition. In addition, you can use a similar approach to check for other types, such as strings, arrays, integers, etc., by passing in their respective classes as arguments to Type::is(). Conclusion Determining whether a Laravel variable is an instance of `Illuminate\Database\Eloquent\Collection` can be achieved through several methods depending on your specific use case. While the initial "ugly" solution might be sufficient for certain cases, using the "better" or "best" solutions ensures accuracy and robustness in your codebase. We encourage you to explore Laravel's documentation and learn more about its powerful type-discovery features as they can make your coding experience easier and more efficient. In summary, this blog post provided a thorough answer to the initial question by presenting various ways to check for Eloquent Collections in Laravel. We also emphasized that relying on the "best" solution using the Type::is() function offers better accuracy and scalability while ensuring your code remains elegant and efficient. Remember, always aim for best practices when working with Laravel's powerful framework!