PHP 7.2 count() function is not working
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
The PHP count() Mystery: Why Your Laravel Code Breaks After Upgrading
As developers, we often find ourselves in the midst of necessary updates. Upgrading PHP versions, like moving from 7.0 to 7.2, is crucial for security and performance. However, these upgrades sometimes expose subtle incompatibilities in code that was perfectly functional on older versions.
Recently, many Laravel developers have encountered a frustrating error: count(): Parameter must be an array or an object that implements Countable. This error pops up when trying to use the simple count() function on the results of Eloquent queries, especially after upgrading PHP.
This post will dive deep into why this happens and, more importantly, show you the correct, idiomatic way to handle counting in Laravel, eliminating the need for complex, brittle workarounds.
Understanding the Error: The Difference Between Data Types
The core of the problem lies not with the count() function itself, but with the type of data you are passing into it. In modern PHP (especially versions like 7.2), functions enforce stricter type checking.
When you execute a query in Laravel, the way you retrieve the data dictates what count() can operate on:
- Retrieving a Single Record: When you use methods like
->first(), Eloquent returns a single Model object ornull. This is a scalar value, not an array or collection, causingcount()to fail because it doesn't implement the necessaryCountableinterface. - Retrieving Multiple Records (The Collection): To use
count(), you must pass it an actual collection of results—an array, anIlluminate\Database\Eloquent\Collection, or any object implementing theCountableinterface.
Your example code demonstrates this perfectly:
$keytest = KeyUser::where('key', '=', $key)->first();
if (count($keytest) == 1) { // Error occurs here because $keytest is a Model object, not an array.
// logic...
}
The fix isn't to create a global function; the fix is to stop trying to count single objects and start leveraging the powerful counting methods provided by the framework itself.
The Laravel Solution: Counting Directly in the Query
Instead of fetching data and then manually counting it, the most efficient and idiomatic approach in Laravel is to let the database engine handle the counting. This is faster, reduces memory usage, and keeps your code cleaner.
If you want to know if a record exists or how many records match a condition, use these built-in methods:
1. Checking for Existence (exists())
If your goal is simply to check if at least one record matches the criteria (which is what your original logic seemed to imply), use exists():
// Check if any user exists with a specific key
$exists = KeyUser::where('key', '=', $key)->exists();
if ($exists) {
// Logic executes only if at least one record was found.
} else {
// Record does not exist.
}
2. Getting the Count Directly (count())
If you explicitly need the number of matching records, use the count() method directly on the query builder instance:
// Get the total number of users matching the criteria
$userCount = KeyUser::where('key', '=', $key)->count();
if ($userCount === 1) {
// Logic executes if exactly one record was found.
}
This approach is superior because it delegates the heavy lifting to the database, which is highly optimized for these operations. When building robust applications, relying on framework features, as promoted by resources like those at laravelcompany.com, ensures you write code that is both functional and maintainable across different PHP versions.
Conclusion
The error you faced is a common symptom of trying to force a non-collection object into a function expecting a collection. The solution isn't patch-work; it’s embracing the framework’s intended design. By shifting your logic from fetching data and then counting it manually (->first() followed by count()) to using Eloquent's built-in methods like exists() or count() directly on the query, you resolve the compatibility issue instantly and write code that is cleaner, more efficient, and fully compatible with modern PHP standards.