Apr 28, 2016
0
Level 4
Laravel 5.2 Storing an array of values in session
I am trying to store values of file paths during the Ajax upload. This is my code:
public function ajaxUpload() {
$name = "";
$path = public_path('files/uploads/articles/');
if (Input::hasFile('file')) {
$files = Input::file('file');
$filePath = [];
foreach($files as $file){
$fP = $path.$file->getClientOriginalName();
Image::make($file)->resize(100, 100)->save($path.'/'.$file->getClientOriginalName());
}
if (Session::has('file.Path')){
Session::push('file.Path', $fP);
Session::save();
}
else {
Session::put('file.Path', $fP);
Session::save();
}
return json_encode(Session::get('file.Path'));
}
But then I get an error of:
LOG.error: Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: [] operator not supported for strings in /home/vagrant/Projects/myProject/vendor/laravel/framework/src/Illuminate/Session/Store.php:411
And when I try like this:
public function ajaxUpload() {
$name = "";
$path = public_path('files/uploads/articles/');
if (Input::hasFile('file')) {
$files = Input::file('file');
foreach($files as $file){
$filePath[] = $path.'/'.$file->getClientOriginalName();
Image::make($file)->resize(100, 100)->save($path.'/'.$file->getClientOriginalName());
}
Session::put('filePath', $filePath);
return json_encode($filePath);
}
return 'string';
}
Then it stores each of the paths separately and not in one array, so I only save one path in DB.
Please or to participate in this conversation.