4
votes

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

5
why store age in session? why not just extract it from auth user when needed?aimme
Is there any way to store my text field value in session when i click on login button or get login successfully. ?Keval Garala
i have posted an answer to do it right wayaimme

5 Answers

5
votes

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

1
votes

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

0
votes

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
}
0
votes

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;
0
votes

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