I'm using laravel 5.3 and in postLogin method, I want to append api_token as query string to url as follows:
http://localhost/Exams/student/dashboard?api_token=4ZtxTu7fAwYpcPYRu46GWmVfncPO0i
My LoginController.php is:
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');
}
}
and routes/web.php is:
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');
});
It appends api_token to dashboard , but $student always comes null in postStudentLogin() method, even though dd($student) gives complete student details.
Please guide me where I'm wrong.
Thanks,
Dipti Sheth
You can pass query string to URL in laravel using named route and controller action. You can pass query string as comma separated array to named route and controller action and redirect to URL.
Retrieving the Request URI The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.
you can easily get current url in laravel 6, laravel 7, laravel 8 and laravel 9 application. $currentURL = Request::url(); dd($currentURL);
//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);
There must be default request parameter,
public function postStudentLogin(Request $request){
$retArr = $request->all();
// try to dd here $retArr variable, you should get everything
}
Give it a try, this should work.
EDIT
To get only query string try this,
Request::getQueryString();
And yes, add this use Illuminate\Http\Request;
to your namespace at the top of file.
Rest, you should get in $request->all()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With