count()参数必须是在laravel中实现可计数的数组或对象

时间:2018-10-27 10:13:04

标签: php laravel

这是这里的代码:

require(dplyr)
test %>%
  select(B, everything())

运行代码时,此错误变为:

  

“ count():参数必须是实现Countable的数组或对象”

8 个答案:

答案 0 :(得分:2)

请注意,在这里,当您使用count()方法时,应该有可数元素,例如数组或对象。

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

但是first()方法只给您一个元素,而不是集合或数组。 get()方法返回可计数的具有找到的元素的集合

除了使用count之外,您还可以直接检查变量本身是否已定义或为空

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

或者您可以使用is_null()方法

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

答案 1 :(得分:1)

您应该检查它是否为null而不是count,因为您要求使用curl http://<your jenkins host>:<your port> 获得一个结果 就是这个

first()

会做到的。

如果使用if($admin) 返回集合,则可以选中->get()

答案 2 :(得分:1)

$admin变量既不是数组也不是可计数的对象。当您使用first()时,如果找到记录,结果将是模型对象,否则将为null。对于这种情况,您可以使用:

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

只需将if (count($admin))替换为if (!empty($admin))

当您使用get()方法获取多条记录时,可以通过以下方式进行检查:

if ($admins->count() > 0) {
    //
}

答案 3 :(得分:1)

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

输出:警告:count():参数必须是数组或对象,该对象或函数在…的第12行中的…上//自PHP 7.2开始

如果条件应为:

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

答案 4 :(得分:1)

发生这种情况是因为在PHP 7.2中count()中的NULL返回警告。 您可以尝试更改

count($admin)

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

答案 5 :(得分: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.
}

答案 6 :(得分:0)

使用isset($admin->id)代替count($admin)

尝试一下:

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');
    }

答案 7 :(得分:0)

向其添加以下代码:

 $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');
        }