Laravel 5.6会话无法重定向到视图

时间:2018-09-04 19:37:32

标签: php laravel laravel-5.6

我正在使用Laravel 5.6并遇到一些问题。如果在数据库中找到的emailpassword匹配,然后重定向,我想对用户进行身份验证。如果用户存在,则重定向到"user1",否则重定向到"/":还要附加迁移文件

路线

Route::any('/user1','UserProfile@store');
Route::get('/login','StudentController@index');

控制器

public function store(Request $request)
{
    $info = array(
        "Email" => $request->get("email"),
        "password" => $request->get("password")
    );

    $info = DB::table("students")->where($info)->first();

    if(!$info){
        $data = array(
            "user_email" => $info->Email,
            "user_password" => $info->password,
            "user_id" => $info->Student_id
        );

        $request->session()->put($data);
        $request->session()->get('user_email');

        //echo $val=$request->session()->get('user_id');die;
        //  print_r($data);die;

        return redirect('/user1');
    } else {
        return redirect('/');
    }
}

迁移

   public function up()
{
    Schema::create('students', function (Blueprint $table) {

        $table->string('Name');
        $table->string('Student_id');
        $table->string('Email');
        $table->string('Department');
        $table->string('Gender');
        $table->string('password');

        $table->timestamps();
    });
}

This Is Picture of Database Table Students

1 个答案:

答案 0 :(得分:0)

if(!$info)是向后逻辑;您正在尝试从$data创建null->first()将返回stdClass对象或null,具体取决于找到的内容,并且该代码只会如果$infonull,则执行)。

应该是

if($info){
    $data = array(
        "user_email" => $info->Email,
        "user_password" => $info->password,
        "user_id" => $info->Student_id
    );

    $request->session()->put($data);
    $request->session()->get('user_email');

    return redirect('/user1');
} else {
    return redirect('/');
}