Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append query string to url in laravel 5.3

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

like image 558
Dipti Sheth Avatar asked Feb 16 '17 05:02

Dipti Sheth


People also ask

How can pass query string in URL in laravel?

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.

How can you retrieve the full URL for the incoming request in laravel?

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.

How do I find URL in laravel 8?

you can easily get current url in laravel 6, laravel 7, laravel 8 and laravel 9 application. $currentURL = Request::url(); dd($currentURL);


2 Answers

//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);
like image 116
sunny kashyap Avatar answered Oct 10 '22 13:10

sunny kashyap


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().

like image 20
Rahul Avatar answered Oct 10 '22 12:10

Rahul