我使用laravel 5.3并且在postLogin方法中,我想将api_token作为查询字符串附加到url,如下所示:
http://localhost/Exams/student/dashboard?api_token=4ZtxTu7fAwYpcPYRu46GWmVfncPO0i
我的LoginController.php是:
public function postStudentLogin(Request $request){
$student = DB::table('students')->where('email', $request->get('email'))->first();
if(Hash::check( $request->get('password'),$student->password))
{
Session::flash('login_message','You have been logged in successfully.');
return redirect()->route('dashboard',['api_token'=> $student->api_token]);
}
else{
return redirect()->back()->withErrors('Incorrect username/password');
}
}
和routes / web.php是:
Route::group(['prefix' => 'student', 'namespace' => 'Auth'], function(){
Route::get('/','Student\LoginController@getStudentHome');
Route::get('/login/{page}',['uses' => 'Student\LoginController@getStudentLogin' ]);
Route::any('/dashboard',['uses' =>'Student\LoginController@postStudentLogin'])->name('dashboard');
});
它将api_token附加到仪表板,但$ student总是在postStudentLogin()方法中为null,即使dd($ student)提供完整的学生详细信息。
请指导我错误的地方。
谢谢,
Dipti Sheth
答案 0 :(得分:1)
必须有默认请求参数
public function postStudentLogin(Request $request){
$retArr = $request->all();
// try to dd here $retArr variable, you should get everything
}
试一试,这应该有用。
修改强>
要获取查询字符串,请尝试此操作
Request::getQueryString();
是的,将此use Illuminate\Http\Request;
添加到文件顶部的命名空间。
休息,你应该进入$request->all()
。
答案 1 :(得分:0)
对于您的代码,可以根据要求使用以下内容。 有关更多推介https://laravel.com/docs/5.3/requests
// Add top of your controller
use Illuminate\Http\Request;
public function postStudentLogin(Request $request){
// To get all data
$inputs = $request->input();
// To get specific value
$api_token = $request->input('api_token');
....
...
}
答案 2 :(得分:0)
//Declare new queries you want to append to string:
$newQueries = ['foo' => 'bar', 'popular'];
//Generate the URL with all the queries:
//Retrieve current query strings with new query variable appended.
$request->fullUrlWithQuery($newQueries);