在laravel 5.6中用数据库检查输入的数据?

时间:2018-10-14 10:40:43

标签: php laravel

我的用户表中有3个数据。

data in users table

登录时我要检查输入的数据以及表中的所有数据。我试过了,但是没用。

  $user = User::where('status',1)->where('deleted_at',null)->get();
      print_r($user); die;

    if(User::where(['type' => $user_type,'user_id' => $user_id,'email' => $email,'password' => $password ,'status'=> 1, 'deleted_at' => null])){
   // if($user_type == $type && $user_id == $uid && $email == $uemail && Hash::check($password,$upass)){

       // echo "Sucess.";die;
        Session::put('userSession',$email);
        Session::put('loginSession',$user_type);

         return view('death_notice.dashboard');

    } else {
       echo "Failed.";die;
       return redirect('/user')->with('flash_message_error','Invalid Login Credentials..');
      }

请帮助我找到解决方案...

3 个答案:

答案 0 :(得分:0)

用于登录检查

use Illuminate\Support\Facades\Auth;

Auth::attempt();

在您的情况下尝试

if(Auth::attempt(['type' => $user_type,'user_id' => $user_id,'email' => $email,'password' => $password ,'status'=> 1, 'deleted_at' => null])){
    // success
} else {
    // error
}

答案 1 :(得分:0)

这有两个问题。

  1. 在第一个if语句中,您正在用户模型上调用->get(),这将导致用户的集合,而不是单个用户。使用->first()

  2. 第二个if语句未调用->first(),因此仅返回查询生成器。

Laravel提供登录功能,您可以向其传递其他参数。

$rememberMe = false;

if (auth()->attempt(['email' => $email, 'password' => $password, 'additional_check' => 'something-else'], $rememberMe) {
  // user is logged in
}

现在我不确定为什么需要两个if语句,它们似乎无关。

答案 2 :(得分:0)

您的if条件有问题。

要达到if条件,您必须传递一个boolean值。

但是您已经传递了一个Query Builder实例。

要从数据库查询中获取布尔输出,可以调用exists()

if(User::where([
    'type' => $user_type,
    'user_id' => $user_id,
    'email' => $email,
    'password' => bcrypt($password) ,
    'status'=> 1,
    'deleted_at' => null])->exists())