【问题标题】:How to write Laravel eloquent query inside model function?如何在模型函数中编写 Laravel 雄辩的查询?
【发布时间】:2019-02-16 19:38:16
【问题描述】:

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

这里是控制器

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

【问题讨论】:

    标签: laravel-5 eloquent model controller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 2021-05-16
      • 2017-05-24
      • 2019-06-27
      • 2014-12-24
      • 2018-01-26
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多