5

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

1
  • you want $student variable on dashboard then use view()->share('student',$student); before redirect statement. I think that might can help you.. Commented Feb 16, 2017 at 8:11

3 Answers 3

8
//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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

8 Comments

I've modified my loginController .php as above , but still $student becomes null and dd($student) gives complete student details
If Hash::check() method verifies hashed password, the it will append query string to url and post page to dashboard. And to append api_token as query string, I'm fetching it from db using $student as: $student = DB::table('students')->where('email', $request->get('email'))->first();
are you getting $request->get('email') here ?
ya..I'm getting it
dd($student): {#199 ▼ +"id": 27 +"email": "[email protected]" +"password": "$2y$10$pu3JMwmCBOAtA5bg05mrl.weMQ1/VNEZ2TSi2Gye5pWLzcrJc1.aO" +"api_token": "4ZtxTu7fAwYpcPYRu46GWmVfncPO0iHzee7NxKqNaY1a9907BoxT1iHl2Azi" +"name": "student" +"address": "pune" +"phone": "6545678932" +"guardian_phone": "" +"enroll": "" +"photo": null +"status": "Active" +"reg_code": "0" +"reg_status": "Live" +"expiry_days": 0 +"renewal_date": null +"presetcode": null +"created_at": "2017-02-08 11:20:44" +"updated_at": "2017-02-08 11:20:44" +"last_login": null }
|
0

For your code, can use below as per requirement. For more refernce 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');
       ....
       ...
}               

1 Comment

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'); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.