0

实际上我不知道如何使用函数从数据库中检索数据并在模型中查询。请帮助我应该在模型函数中添加什么。

这里是控制器

public function checkAdmin(Request $request){
        $data  = array();
        $data['email'] = $request->email;
        $data['password'] = $request->password;
        $found = AdminModel::checkAdmin($data);
        if($found == TRUE){
            echo "found";
        }
        else{
            echo "sorry";
        }
    }

这是模型函数

public static function checkAdmin($data){
        $found = $this->where('email',$data['email'])->where('password',$data['password'])->first();
        return $found;
    }
4

1 回答 1

0

checkAdmin该函数的目的是什么?您是否正在尝试验证登录?如果是,Laravel 会通过以下Auth::attempt方法为您执行此操作:

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) {
    // Authentication passed...
    return redirect()->intended('dashboard');
}

https://laravel.com/docs/5.7/authentication#authenticating-users

但是,要回答您的问题,您可以这样做:

$userFound = AdminModel::where(['email' => $request->email, 'password] => $request->password)->count();

if($userFound){
    echo "found";
}
else{
    echo "sorry";
}
于 2019-02-17T22:45:06.067 回答