Laravel How to store extra data/value in session Laravel

倾然丶 夕夏残阳落幕 提交于 2019-12-20 02:27:36

问题


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


回答1:


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




回答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




回答3:


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
}



回答4:


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;



回答5:


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



来源:https://stackoverflow.com/questions/37597273/laravel-how-to-store-extra-data-value-in-session-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!