laravel sessions

 session used to store and pass informatoon from one page to another temporarily until user close the website

https://laravel.com/docs/8.x/session#main-content

its temporarily

laravel seesion can be saved in datrabase, file, encrypted cookies


config/session.php

'lifetime' => env('SESSION_LIFETIME', 120),

'expire_on_close' => false,


we can connect to requred database using database.php

mongodb

we can store into database instaed of files and changing some drivers

php artisan session:table


interacting with session

first by request instance and 

second method

using global helper method session


session actions



$request->session()->put('course','laravel');
session([;course'=>'advanced php']);
$request->session()->get('course');

session()->all();

has  -> if($request-> session()-> has('learnvern'))

{

echo 'exists';}

learnver key exists and not null above works

if key exists and null or not null below works


 if($request-> session()-> exists('learnvern'))

{

echo 'exists';}

missing



exist

missing -> if key is not present - to deternmine it is used



push to arra sesion value

$request->session()->push('user.teams', 'developers');


retrieve and delete

$value = $request->session()->pull('key', 'default');

$value = $request->session()->forget('key');


incrimenting and decriment


may use the increment and decrement methods:

how many tomes refresh 


$request->session()->increment('count');
 
$request->session()->increment('count', $incrementBy = 2);
 
$request->session()->decrement('count');
 
$request->session()->decrement('count', $decrementBy = 2);

flash data

it wil store upto next request , after that it will delete

$request->session()->flash('status', 'Task was successful!');



regenreate the session

for every time authetnication regenearte the session


$request->session()->regenerate();

before regenerate delete the data iusing ivalidate

$request->session()->invalidate();



session flaseh

for example payment over, sucesfule message wil show upto next request

$request->session()->flash('msg','welcome to learn vern');


if (session()->has('sucess')

{{ sesion('success') }}


reflash method

$request->session()->reflash();

till close window


$request->session()->keepp('msg','siccess');

@if ($message = Session::get('success'))
@endif



  1. _____ provide a way to store information about the user across multiple requests.

    Marked Answer : session

    Correct Answer : session

  2. Which session driver is not supported in Laravel?

    Marked Answer : dynamodb

    Correct Answer : None of the above

  3. file,redis,dynamodb

  4. Which helper is used to work with session data?

    Marked Answer : session()

    Correct Answer : session()

  5. How to retrieve all the data in the session?

    Marked Answer : $request->session()->all();

    Correct Answer : $request->session()->all();

  6. Which is the correct way to store data in the session?

    Marked Answer : session(['key' => 'value']);

    Correct Answer : A and B both

  7. Data stored in the session using ______ method will be available immediately and during the subsequent HTTP request.

    Marked Answer : flash()

    Correct Answer : flash()























No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...