简体   繁体   English

如何更新laravel 4中的会话数组 - 使用新项更新

[英]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. 我正在使用ajax和laravel 4,我上传的文件是数组 - 它们是多个。 But I'm not sure how to update session array withe the new array. 但我不知道如何使用新数组更新会话数组。 I've tried array_merge but it's overwrited. 我已经尝试过array_merge,但它已经过时了。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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