count() parameter must be an array or an object that implements countable in laravel

count() parameter must be an array or an object that implements countable in laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the count() Parameter Error in Laravel Queries

Encountering the PHP fatal error count(): Parameter must be an array or an object that implements Countable is a common stumbling block when transitioning from procedural logic to modern Laravel query scopes. This specific issue often arises from a fundamental misunderstanding of how the Query Builder returns data versus how scalar functions expect their arguments.

The root cause in your scenario lies in this line of code:

$admin = Admin::where('email', $request->email)->first();
if (count($admin)) { ... }

When you invoke Admin::query()->first(), the Query Builder executes the SQL statement and retrieves a single model instance. Crucially, this returns an object of type Illuminate\Database\Eloquent\Model (or your specific Admin model), not a collection or an array. The built-in PHP function count() strictly adheres to its contract: it expects either an array or an object that implements the Countable interface. Since the standard Eloquent Model does not implement this interface, passing $admin directly to count() triggers the fatal error you are seeing.

The Correct Approach: Boolean Evaluation

In the context of Laravel and Eloquent, checking for the existence of a record is almost always more efficient and cleaner than attempting to count it. A model instance returned by first(), find(), or any query method that targets a single row represents a successful retrieval. Conversely, if no record matches the criteria, these methods return null.

You can leverage this behavior directly in your conditional logic without needing to invoke count() at all:

$admin = Admin::where('email', $request->email)->first();

if ($admin) {
    // The admin exists
    if ($admin->status == 0) {
        return ['email' => 'inactive', 'password' => 'You are not an active person, Please contact to admin'];
    } else {
        return ['email' => $request->email, 'password' => $request->password, 'status' => 1];
    }
}

// The admin does not exist
return $request->only($this->username(), 'password');

By removing the count() wrapper, you rely on PHP's truthiness evaluation. Since $admin is an object when the query succeeds and null when it fails, this check is both idiomatic and performant. There is no overhead associated with counting a single item versus checking its existence.

Understanding Countable in Laravel

It is worth noting that you can count model instances if you explicitly request a collection. If your logic required knowing the total number of active admins, for example, you would need to use the get() method without first(), which returns a Collection:

$admins = Admin::where('email', $request->email)->get(); // Returns Collection
$count = count($admins); // Valid: Collection implements Countable

Or, if you prefer not to load the models into memory for a simple count query, you can utilize the count() method provided by the Query Builder itself:

$adminCount = Admin::where('email', $request->email)->count();

However, in your specific case, since you are validating login credentials or user status based on a single entity, retrieving that one entity and checking for null is the appropriate pattern. Attempting to wrap an Eloquent model instance in count() conflates data retrieval with existence checking, leading to the runtime error observed.

A Note on Security and Logic

While fixing the immediate error is the priority, it is important to review the logic flow within your credentials method. The original code returns a response array containing a raw password field ('password' => $request->password) only when the admin exists and is active. In a real-world authentication scenario, you would typically validate the hashed password against the stored hash and never expose the raw input in a JSON response intended for client-side handling unless specifically managing a custom login flow that expects it. Additionally, ensure that $this->username() returns valid arguments for the only method; passing a method name as a string argument to Request::only is generally incorrect if the intent was to get specific attributes from the request object, which usually requires accessing the underlying array or using the input() helper correctly.

Addressing the count() error resolves the immediate crash, but ensuring your authentication logic aligns with security best practices will prevent future vulnerabilities in your application.

Our network