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?

What is the usage of session in laravel?

WBOY
WBOYOriginal
2022-02-23 16:29:503977browse

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');".

What is the usage of session in laravel?

#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.

What is the usage of session in laravel

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(&#39;key4&#39;, &#39;name_&#39;.$i);
}

session temporary data (data can only be accessed once)

session()->flash(&#39;key5&#39;, &#39;value5&#39;);
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(&#39;key5&#39;, &#39;default_value&#39;);

3. Clear or delete the data

Delete data based on key and return

session()->pull(&#39;key3&#39;);

Delete key

session()->forget(&#39;key3&#39;);

Clear all sessions

session()->flush();

4. Determine whether the session exists

session()->has(&#39;key4&#39;)

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn