Is it possible to store an array as flash data in Laravel?
Asked Answered
A

4

11

I have been looking around for a way of doing this. I know it is possible to store an array in the session with the following: Session::push('user.teams', 'developers');

Is it possible to do the same but with flash data? Something like Session::flashpush('user.teams', array('developers', 'designers')); would be great.

The usecase for me at this moment is primarily the following:

Session::flash('flash_message', $validator->messages());

Ackler answered 4/11, 2013 at 21:48 Comment(4)
I am not a Flash developer, but you probably have to pass the data to a Flash object on the site which then stores the values.Knar
I am not talking about Flash as in Adobe Flash. Flash data that expires the next request. I will edit my question to further explain this.Ackler
Oh, I am totally sorry :) I should read questions more carefully.Knar
serialize php.net/manual/en/function.serialize.phpBigamist
W
14

As far as I know, you can do it. I've checked this just in case:

Session::flash('test', array('test1', 'test2', 'test3'));

... After the request

dd(Session::get('test'));

// array(2) { [0]=> string(5) "test1" [1]=> string(5) "test2" [2]=> string(5) "test3" }

It works. Also you can serialize an array or object as Christopher Morrissey just commented

Waltz answered 4/11, 2013 at 21:59 Comment(0)
H
7

I use this:

Session::flash($key, array_merge((array)Session::get($key), array($value)));
Hyo answered 24/4, 2014 at 20:31 Comment(0)
B
5

I created this helper class:

<?php

class Flash {
    public static function push($key, $value) {
        $values = Session::get($key, []);
        $values[] = $value;
        Session::flash($key, $values);
    }
}

It allows you push multiple items to the same key such that it always return an array when fetched.

Usage:

 Flash::push('success','Feature saved');

Twig template (Blade shouldn't be too much different):

{% if session_has('success') %}
    <div class="alert alert-block alert-success fade in">
        <button class="close" data-dismiss="alert">&times;</button>
        {% for msg in session_get('success') %}
            <p><i class="fa-fw fa fa-check"></i> {{ msg }}</p>
        {% endfor %}
    </div>
{% endif %}

In your scenario, you would probably use it like this:

Flash::push('flash_message', 'user.teams');
Flash::push('flash_message', 'developers');
Beasley answered 26/4, 2014 at 21:12 Comment(0)
P
0

Laravel 8

This is the most efficient way I've found using the latest version of Laravel if you want to store info input by the user as an array and then pull it separately. I'm using for a Madlibs app.

$req->session()->put([
  'adj1' => $req->input('adj1'),
  'noun1' => $req->input('noun1'),
  'place' => $req->input('place'),
]);

return $req->session()->flash('test',
   array('adj1', 'noun1', 'place',
));

I hope this helps a future users.

Pavel answered 28/4, 2021 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.