2015-11-30 73 views
1

有人可以帮助我如何使用链接到第二个表的student_id登录。例如;第二个表的id为'abc',所以我想以'abc'登录,当它发布到下一页时,它将检索链接到'abc'数据的数据而不是所有数据。登录第二个表格的数据

AuthController.php

public function getLogin() 
{ 
    return view('auth.login'); 
} 

public function postLogin(Request $request) 
{ 
    if($this->auth->attempt($request->only('student_id', 'password'))) 
    { 
     return redirect('/student/profile'); 
    } 

    return redirect('/auth/login') 
     ->withErrors([ 
      'student_id'=>'Invalid User ID' 
     ]); 
} 

route.php

Route::get('/', function() { 
    return View::make('auth/login'); 
}); 

Route::get('student/profile', 
    ['middleware' => 'auth','uses' =>'Auth\[email protected]']); 

Route::post('/student/profile',array(
    'as'=>'login', 
    'uses'=>'[email protected]' 
)); 

login.blade.php

<section id="container"> 
    {!! Form::open(array('action' => 'Auth\[email protected]')) !!} 
    {!! csrf_field() !!} 

    <p>Student ID : {!! Form::text('student_id',null)!!}</p> 
    <p>Password : {!! Form::password('password')!!}</p> 

    <p>{!! Form::submit('Login') !!} </p> 
    {!! Form::close()!!} 
</section> 
+0

你能否详细说明你的问题多一点 –

+0

当我登录的ID:ABC,PWORD:ABC。它将移动到名为profile的下一页。在配置文件表中,有许多数据具有不同的id。所以,我只想要链接到ID:abc的数据。当我登录为'尝试'时,我只想检索数据配置文件,在表格配置文件中有'尝试'..这里有两个表名为用户(用于登录)和配置文件(用于显示数据)。 –

+0

用户成功登录后,您可以使用'Auth :: user();'获取用户详细信息,以便您可以使用它来检索登录用户的'id',然后使用此'id'检索配置文件信息'with'。 –

回答

0

你的路线不正确(不符合逻辑)。

Route::get('/', function() { 
    // when user is already login 
    if (Auth::user()) { 
     return redirect('students/profile'); 
    } 

    return redirect('login'); 
}); 

Route::get('login', ['uses' =>'Auth\[email protected]']); 
Route::post('login', ['as' => 'login','uses' =>'Auth\[email protected]']); 
Route::post('student/profile',['middleware' => 'auth', 'uses'=>'[email protected]' ]); 

后一个学生登录该getProfile控制器的方法可以是这样的:

public function getProfile() { 
    // get data from user thats login 
    $user = Auth::user(); 

    $userData = ModelForTableTwo::where('student_id', $user->id)->get(); 

    // more stuff 

    return View::make('path.to.view', compact('userData', 'user')); 
} 
+0

为什么它直接去页面学生/个人资料? –

+0

因为有一个检查用户是否登录,当用户登录时重定向到学生的个人资料页面 –

+0

我得到变量ID:未定义。 –

相关问题