5

I'm using default auth() in laravel login (email & password) Now i try to take input from the user in text field like (Age or City)

Now i want to store (Age/City) in my session.

Help me

3
  • why store age in session? why not just extract it from auth user when needed? Commented Jun 2, 2016 at 16:35
  • Is there any way to store my text field value in session when i click on login button or get login successfully. ? Commented Jun 2, 2016 at 16:42
  • i have posted an answer to do it right way Commented Jun 2, 2016 at 17:09

5 Answers 5

6

You can use session() helper:

session('age', 18); // saves age into session

$age = session('age')`; // gets age from session

Update

If you want to save Age and City after user registration, you should store this data in a DB, not in a session. You can add some fileds in create method of app\Http\Controllers\Auth\AuthController.php

Sign up to request clarification or add additional context in comments.

5 Comments

But where do i have to write. I'm new in laravel
Wherever you want: controller's method, Blade template, middleware etc. It depends on what are you trying to do.
Alexey i need this value when auth value get and check for login. Which file in laravel to check user and password is match or not ?
@KevalGarala, please look at update section of my answer
Is there any way to store my text field value in session when i click on login button or get login successfully. ?
2

You can use

Session::put('key', 'value');

To get key from Session use

Session::get('key');

You can use the session() helper function as @Alexey Mezenin answer.

Laravel Session Documentation

7 Comments

But where do i have to write. I'm new in laravel
Have you added the age/city in your login form?
Yes i added this in my login form
You need to override postLogin() in AuthController refer to this stackoverflow.com/questions/30656113/laravel5-custom-login it may help you.
No i don't want to do that. Is there any way to store my text field value in session when i click on login button or get login successfully. ?
|
1

Ok let me enlighten you. if you want to store it in session do it this way.

session('country', $user->country); // save
$country = session('country')`; // retrieve

But that is not the way we do in Laravel like frameworks, it uses models

once the user is authenticated each time when we refresh the page, application looks for the database users table whether the user exists in the table. the authenticated user model is a user model too. so through it we can extract any column. first thing is add extra fields to the User class(Model) $fillable array. so it would look something like this.

User.php

protected $fillable = ['username', 'password', 'remember_token', 'country'];

so after simply logging in with user name and password in anywhere just use Request class or Auth facade. Facades are not too recommended so here for your good as a new one i would just say how to use Request. Suppose you want to retrieve your Authenticated user country inside TestController.php here is how it could be used in the methods.

TestController.php

use Illuminate\Http\Request;

public function testMethod(Request $request)
{
   $someCountry = $request->user()->country; //gets the logged in user country
   dd($someCountry); //dd is die and dump, could be used for debugging purposes like var_dump() method
}

1 Comment

It's very true. But i don't want to store country in database.
0

Using Request

public function ControllerName (Request $request){

   $request->session()->put('session_age', $age);

}

Get session_age

$get_session_age = $request->session()->get('session_age');

Using Session

   public function ControllerName (){

       Session::put('age',$age);

    }

Get the session

$session_age = Session::get('age');

Don't forget to define Session or Request in your controller!!!

use App\Http\Requests;
use Session;

Comments

0

To work with session in your controller you need to include session first in your controller

use Session;

After that for store data in session. There is several ways to do it. I prefer this one (in controller)

session()->put('key',$value);

To display session data in your View you can do it like this

@if(Session::has('key'))
I'v got session data
@else
I don't have session data
@endif

To get session data in your Controller you can do it like this

session()->get('key')
//or
session()->get('key','defaul_value_if_session_dont_exist')

When you are done with your data in session you can delete it like this (in controller)

session()->forget('key');

All this basic usage of session is well documented in official Laravel documentation here.

Hope it helps you

Comments

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.