php count() 参数必须是数组或在laravel中实现countable的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53020833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
count() parameter must be an array or an object that implements countable in laravel
提问by faraz
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"
“count(): 参数必须是一个数组或一个实现 Countable 的对象”
回答by Jericho Manalo
This is my solution:
这是我的解决方案:
count(array($variable));
hope it works!
希望它有效!
回答by Dmytro Huz
It happens because of in PHP 7.2 NULL in count() return Warning. You can try to change
发生这种情况是因为在 PHP 7.2 中 count() 返回警告为 NULL。你可以尝试改变
count($admin)
to
到
count((is_countable($admin)?$admin:[]))
回答by Teoman T?ng?r
Note that here, When you use the count()
method, there should be countable element, like an array or object.
注意这里,当你使用count()
方法时,应该有可数元素,比如数组或对象。
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
但是该first()
方法为您提供单个元素,而不是集合或数组。该get()
方法返回一个包含找到元素的可数集合
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
或者你可以使用is_null()
方法
if(!is_null($admin)){
// do something here
}
回答by Ali Farhoudi
$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:
$admin
变量既不是数组也不是实现可数的对象。当您使用时first()
,如果找到记录,结果将是一个模型对象,否则它将为空。对于这种情况,您可以使用:
if (!empty($admin)) {
//
}
Just replace if (count($admin))
with if (!empty($admin))
.
只需替换if (count($admin))
为if (!empty($admin))
.
And when you use get()
method to get multiple records you can check by:
当您使用get()
方法获取多条记录时,您可以通过以下方式进行检查:
if ($admins->count() > 0) {
//
}
回答by rajesh
$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
输出:警告:count():参数必须是一个数组或一个对象,在第 12 行中实现了 Countable // 自 PHP 7.2 起
if condition should be like:
如果条件应该是这样的:
if(isset($admin) && count($admin))
回答by sonu pokade
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.
}
回答by nakov
You should check if it is null instead of count, because you ask for one result with first()
just this
您应该检查它是否为空而不是计数,因为您first()
只需要一个结果
if($admin)
will do it.
会做的。
if you use return a collection using ->get()
then you can check $admin->count()
.
如果您使用返回一个集合,->get()
那么您可以检查$admin->count()
.
回答by paranoid
Use isset($admin->id)
instead of count($admin)
使用isset($admin->id)
代替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');
}
回答by Arif Nofriadi
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');
}