count() parameter must be an array or an object th

2020-04-07 15:40发布

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"

标签: php laravel
9条回答
Lonely孤独者°
2楼-- · 2020-04-07 16:35

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');
        }
查看更多
一夜七次
3楼-- · 2020-04-07 16:36
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.
}
查看更多
三岁会撩人
4楼-- · 2020-04-07 16:38

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().

查看更多
登录 后发表回答