GHOST
GHOST

Reputation: 59

session and session flash in laravel

I am really confused on difference between storing in session flash and flash and what type of case we need session flash . I studied on laravel documentation but didnt get it .

I am new to laravel .Any help is truly appreciated .

Upvotes: 3

Views: 1208

Answers (2)

Andreasfi
Andreasfi

Reputation: 362

An example: A user fills out the contact form and you want to give him a confirmation that the message has been sent correctly. So you redirect him to the landing page and store this message in the session. When the page loads, the message is displayed.

i.eg.

return redirect('contactform/')->with('message','Your message has been sent!');

Or you can set the flash message just before the redirection code:

$request->session()->flash('message', 'Your message has been sent!');
return redirect('contactform/');

You can then display the message like this:

@if (session('message'))
  <div class="message-has-been-sent-confirmation">
      {{ session('message') }}
  </div>
@endif

Now you can apply this to any case: confirmations, errors, welcome message etc.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

From the docs:

Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted.

So, if you want to store the data only for the next request, use the flash()method and if you want to store it for all requests, use:

session(['key' => 'value'])

Upvotes: 1

Related Questions