I'm using Laravel 4.2. I have a method for storing files and I'm trying to push items in session array. Every time user clicks on a button, I want to save these file names in session array with a name and later I should get them.
I'm trying with the following code:
$name = Input::get('name'); $input = Input::all(); foreach ($input[$name] as $key => $corporate) { $file_name = $key.'_'.$current_time . '_' . $corporate->getClientOriginalName(); //code for uploading files if(Session::has($name)) { Session::push($name.".".$key, $file_name); //I want to add new items in this array } else { Session::put($name, $file_name); //for first image } }
As I read from laravel docs:
Push A Value Onto An Array Session Value
Session::push('user.teams', 'developers');
);`
But it doesn't add new items to array, it overwrites it.
After first image upload in session array I have:
["director_front_passport[]"]=> array(1) { [1]=> array(1) { [0]=> string(54) "0_1472669237_12894473_678457885627912_1258115018_o.jpg" } }
After uploading second image, session is:
["director_front_passport[]"]=> array(1) { [2]=> array(1) { [0]=> string(33) "0_1472669255_animated_loading.gif" } }
What is the proper way to push items in session array with definite name, I mean I should get these items later using: Session::get('name') for example.
You can try the following code:
$name = Input::get('name');
$input = Input::all();
$items = Session::get($name, []);
foreach ($input[$name] as $key => $corporate) {
$file_name = $key.'_'.$current_time . '_' . $corporate->getClientOriginalName();
if (!array_key_exists($key, $items)) {
$items[$key] = [];
}
$items[$key][] = $file_name;
}
Session::put($name, $items);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.