Laravel How to store extra data/value in session Laravel

后端 未结 5 625
我寻月下人不归
我寻月下人不归 2021-01-20 23:11

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) i

5条回答
  •  自闭症患者
    2021-01-20 23:18

    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

提交回复
热议问题