简体   繁体   中英

How to update session array in laravel 4 - updated with new items

I'm using ajax and laravel 4 and I upload files that are arrays - they are multiple. But I'm not sure how to update session array withe the new array. I've tried array_merge but it's overwrited.

My method looks like that:

 public function storeFiles() { $name = Input::get('name'); $input = Input::all(); $current_time = time(); $trim_name = trim($name, '[]'); $corporate_docs = array(); foreach ($input[$trim_name] as $corporate) { $file_name = $current_time . '_' . $corporate->getClientOriginalName(); $corporate->move(APPLICATIONS_DIR, $file_name); $corporate_docs[] = $file_name; } $session = Session::get($name); if($session) { $docs = array_push($session, $corporate_docs); Session::put($name, $docs); } else { Session::put($name, $corporate_docs); } } 

How should I merge existing session array withe the newly created?

Updated: my code now is:

  $trim_name = trim($name, '[]'); $corporate_docs = array(); foreach ($input[$trim_name] as $corporate) { if ($corporate) { $file_name = $current_time . '_' . $corporate->getClientOriginalName(); $corporate->move(APPLICATIONS_DIR, $file_name); $corporate_docs[] = $file_name; } } $session = Session::get($name); if (Session::has($name)) { $docs = array_merge($session, $corporate_docs); //dd($docs); Session::forget($name); Session::put($name, $docs); dd(Session::get($name)); } else { Session::put($name, $corporate_docs); } 

But it is everwwriten again. When I add first image, it is stored in session and when I add another, it is merged with the first array but when I add third image, in the newly created session array - it is first image and last image. Second image is overwriten.

Try this:

if(session()->has($name)) {
    session()->forget($name);
}

session()->put($name, $corporate_docs);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM