Home > PHP Framework > Laravel > body text

What is the usage of session in laravel?

WBOY
Release: 2022-02-23 16:29:50
Original
3947 people have browsed it

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');
Copy after login

Storage array

for ($i=1;$i<=10;$i++) {
    session()->push(&#39;key4&#39;, &#39;name_&#39;.$i);
}
Copy after login

session temporary data (data can only be accessed once)

session()->flash(&#39;key5&#39;, &#39;value5&#39;);
Session()->reflash();//在all()、get()等方法前调用该方法,闪存数据会一直保存
Copy after login

2. Get data

Get all data

session()->all();
Copy after login

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;);
Copy after login

3. Clear or delete the data

Delete data based on key and return

session()->pull(&#39;key3&#39;);
Copy after login

Delete key

session()->forget(&#39;key3&#39;);
Copy after login

Clear all sessions

session()->flush();
Copy after login

4. Determine whether the session exists

session()->has(&#39;key4&#39;)
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!