'count() parameter must be an array or an object that implements countable in laravel
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"
Solution 1:[1]
This is my solution:
count(array($variable));
hope it works!
Solution 2:[2]
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:[]))
Solution 3:[3]
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
}
Solution 4:[4]
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().
Solution 5:[5]
$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) {
//
}
Solution 6:[6]
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.
}
Solution 7:[7]
$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))
Solution 8:[8]
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');
}
Solution 9:[9]
count((array)$variable);
It works like a charm.
Solution 10:[10]
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');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | nakov |
| Solution 5 | Ali Farhoudi |
| Solution 6 | sonu pokade |
| Solution 7 | rajesh |
| Solution 8 | |
| Solution 9 | arijitdas7 |
| Solution 10 | Stefan Becker |
