如何在模型函数内部编写Laravel雄辩的查询?

时间:2019-02-16 19:38:16

标签: laravel-5 eloquent model controller

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

这是控制者

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

1 个答案:

答案 0 :(得分: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";
}