put('key1', 'value1')"; 2. Get data, the syntax is "session()->all()"; 3. Clear or delete Data, the syntax is "session()->pull('key3');"."/> put('key1', 'value1')"; 2. Get data, the syntax is "session()->all()"; 3. Clear or delete Data, the syntax is "session()->pull('key3');".">
Home > Article > PHP Framework > What is the usage of session in laravel?
Usage: 1. Store data, the syntax is "session()->put('key1', 'value1')"; 2. Get data, the syntax is "session()->all( )"; 3. Clear or delete data, the syntax is "session()->pull('key3');".
#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.
1. Store data
Store a single piece of data. The following two writing methods have the same function. Use session( ) as an example to demonstrate
$request->session()->put('key1', 'value1'); session()->put('key2', 'value2');
Storage array
for ($i=1;$i<=10;$i++) { session()->push('key4', 'name_'.$i); }
session temporary data (data can only be accessed once)
session()->flash('key5', 'value5'); Session()->reflash();//在all()、get()等方法前调用该方法,闪存数据会一直保存
2. Get data
Get all data
session()->all();
Get a single piece of data based on the key, the second parameter is the default value
session()->get('key5', 'default_value');
3. Clear or delete the data
Delete data based on key and return
session()->pull('key3');
Delete key
session()->forget('key3');
Clear all sessions
session()->flush();
4. Determine whether the session exists
session()->has('key4')
[Related recommendations: laravel video tutorial]
The above is the detailed content of What is the usage of session in laravel?. For more information, please follow other related articles on the PHP Chinese website!