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

Viewed 145355

This is code here:

protected function credentials(Request $request)
{
    $admin=admin::where('email',$request->email)->first();
    if(count($admin))
    {
       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];
           }
       }
       return $request->only($this->username(), 'password');
    }

When i run the code this error become:

"count(): Parameter must be an array or an object that implements Countable"

11 Answers

This is my solution:

count(array($variable));

It happens because of in PHP 7.2 NULL in count() return Warning. You can try to change

count($admin)

to

count((is_countable($admin)?$admin:[]))

Note that here, When you use the count() method, there should be countable element, like an array or object that implement ArrayAccess.

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

But the first() method give you single element, not a collection or array. The get() method returns you countable a collection with found elements

Instead of using count you can directly check variable itself is it defined or null

if($admin){
  // do something here
}

or you can use is_null() method

if(!is_null($admin)){
  // do something here
}

You should check if it is null instead of count, because you ask for one result with first() just this

if($admin)

will do it.

if you use return a collection using ->get() then you can check $admin->count().

$admin variable is neither array nor object that implements countable. When you use first() the result will be a model object if record is found else it will be null. For this condition you can use:

if (!empty($admin)) {
    //
}

Just replace if (count($admin)) with if (!empty($admin)).

And when you use get() method to get multiple records you can check by:

if ($admins->count() > 0) {
    //
}
Well,
$admin=Admin::where('email',$request->email)->first();
//It will always return an **object**.
And make sure you included Admin model in your controller like as.
Use App\Admin;
at the same time check that you will have to mention which field of table needs to be fillable like in your model such as 
protected $fillable = [
'first_name',
'last_name'
];

whatever data you will going to save in your database.
and then check object is null or not
I mean is.

if($admin && $admin!==null){
  //do whatver you want to do.
}

You can cast the variable to an array:

count((array)$variable);

If it's null then it will become an empty array.

$admin = null;
var_dump(count($admin));

output: Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12 // as of PHP 7.2

if condition should be like:

if(isset($admin) && count($admin))

Use isset($admin->id) instead of count($admin)

Try this :

protected function credentials(Request $request)
{
    $admin=admin::where('email',$request->email)->first();
    if(isset($admin->id)))
    {
       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];
           }
       }
       return $request->only($this->username(), 'password');
    }

In my case count was 1 even when I got [] from api

so I had to put it in try catch

try{
 $propertyId = arr[0].propertyId;
}catch(\Exception $e) {
return response()->json(['success' => 'false']);
}

add this your controler this code:

 $user = User::where('email',$request->email)->first();
        if ($user){
            return redirect()->back()->with('errors','We cant find a user with that e-mail address.');
        }else{
            $user->password = bcrypt($request->new_password);
            $user->update();
            return redirect()->back()->with('success','Success');
        }
Related