我想从DB :: Select get()获取一个'INT'值

时间:2019-07-08 14:12:25

标签: php mysql

我想进行简单的登录并将每个用户重定向到其私人页面,因此每个用户将拥有自己的页面。像www.example.com/user1。

这是针对我正在为此工作开发的新应用程序的。

public function login(request $req){
    $username = $req->input('username');
    $password = $req->input('password');
    $type = 1;
    $checklogin = DB::table('users')->where(['username'=>$username,'password'=>$password])->get();
    $checktype = DB::table('users')->where(['username'=>$username,'password'=>$password,'type'=>$type])->get();        
    $url2 = DB::table('users')->where(['username'=>$username,'password'=>$password])->get('id');
    if(count($checklogin) >0){
        if (count($checktype) ==1) {
            header("Location: /admin/$url2", true, 301);
            exit();
        }else{
            if ($url2 == 4) {              
                echo "$url2";
            }
        }
    }else{
        return view('loginfailed');
    }
}
}

要重定向的网址是:“ http://localhost:8000/admin/[%7B%22id%22:2%7D]

2 个答案:

答案 0 :(得分:1)

您可以将id提取为

// fetching 1 record and then id of it
$url2 = DB::table('users')->where(['username'=>$username,'password'=>$password])->first()->id;

答案 1 :(得分:0)

public function login(request $req){
    $username = $req->input('username');
    $password = $req->input('password');
    $checklogin = DB::table('users')->select('type')->where(['username'=>$username,'password'=>$password])->get()->first();

    if(count($checklogin){
        if ($checklogin->type ==1) {
            header("Location: /admin/$url2", true, 301);
            exit();
        }elseif ($checklogin->type == 4) {              
                //do something else
        }
    }else{
        return view('loginfailed');
    }
}
}