Multiple upload doesn't store in storage link and database. Hi guys I have data from my view but doesn't store in my storage link and database..
this is my data in dd
array:4 [▼
0 => Livewire\TemporaryUploadedFile {#1437 ▼
+"disk": "local"
#storage: Illuminate\Filesystem\FilesystemAdapter {#1383 ▶}
#path: "livewire-tmp/74oUpPl6StKfuI6EtH3Rdbb2JYaqE7-metaRmFjZWJvb2stMDAxOS5qcGc=-.jpg"
-test: false
-originalName: "74oUpPl6StKfuI6EtH3Rdbb2JYaqE7-metaRmFjZWJvb2stMDAxOS5qcGc=-.jpg"
-mimeType: "application/octet-stream"
-error: 0
#hashName: null
path: "C:\Windows\Temp"
filename: "74oUpPl6StKfuI6EtH3Rdbb2JYaqE7-metaRmFjZWJvb2stMDAxOS5qcGc=-.jpg"
basename: "php2B64.tmp"
pathname: "C:\Windows\Temp\php2B64.tmp"
extension: "tmp"
realPath: "C:\wamp64\www\cbcc-website\storage\app\livewire-tmp/74oUpPl6StKfuI6EtH3Rdbb2JYaqE7-metaRmFjZWJvb2stMDAxOS5qcGc=-.jpg"
size: 554154
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
1 => Livewire\TemporaryUploadedFile {#1438 ▶}
2 => Livewire\TemporaryUploadedFile {#1436 ▶}
3 => Livewire\TemporaryUploadedFile {#1431 ▶}
]
and this is my store function
public function store(){
dd($this->gallery_image);
$action = '';
$data = $this->validate([
'gallery_image.*' => 'image|max:1024',
]);
if( !empty( $this->gallery_image ) ){
$gallery_image = 'gallery_image.' . time() . $this->gallery_image->getClientOriginalName();
foreach( $this->gallery_image as $images ){
$images->store('public/slides-images', $gallery_image);
}
$data['gallery_image'] = $gallery_image;
}
if($this->galleryId){
Gallery::find($this->galleryId)->update($data);
$action = 'edit';
}else{
Gallery::create($data);
$action = 'store';
}
$this->emit('showEmitedFlashMessage', $action);
$this->resetInputFields();
$this->emit('refreshParent');
$this->emit('closeGalleryModal');
}
Try
if (!empty($this->gallery_image)) {
$images = $this->gallery_image;
$paths = [];
foreach ($images as $image) {
$paths[] = Storage::putFileAs('public/slides-images', $image, 'gallery_image_' . time() . $image->getClientOriginalName());
}
}
What is not working? The file is not saved into the storage folder? or there is a 500 internal server error?
@frankielee
In my case my file upload is not working in production. while in localhost it is working properly. can you help me to figure out this error.
[2021-08-06 09:23:41] production.ERROR: Undefined offset: 0 {"userId":74,"exception":"[object] (ErrorException(code: 0): Undefined offset: 0 at /home/myDomainName/public_html/myProjectName/vendor/livewire/livewire/src/WithFileUploads.php:37)
[stacktrace]
I create a new thread related to this error. Plz look at this.
https://laracasts.com/discuss/channels/laravel/file-upload-cause-an-error-in-production
I got this new error... I tried to upload one image.
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\wamp64\www\cbcc-website\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 886
which column is cast as an array?
I will show you the whole code
public $gallery_image = []; // this from above
//this is the store function
public function store(){
// dd($this->gallery_image);
$action = '';
$data = $this->validate([
'gallery_image.*' => 'image|max:1024',
]);
if (!empty($this->gallery_image)) {
$images = $this->gallery_image;
$paths = [];
foreach ($images as $image) {
$paths[] = Storage::putFileAs('public/slides-images', $image, 'gallery_image_' . time() . $image->getClientOriginalName());
}
}
if($this->galleryId){
Gallery::find($this->galleryId)->update($data);
$action = 'edit';
}else{
Gallery::create($data);
$action = 'store';
}
$this->emit('showEmitedFlashMessage', $action);
$this->resetInputFields();
$this->emit('refreshParent');
$this->emit('closeGalleryModal');
}
and this is the input file
@if ($errors->has('gallery_image.*'))
{{$errors->first('gallery_image')}}
@endif
```
you should have to know by yourself.
you are trying to pass an array of file upload objects into the gallery create or update
you are also not doing anything with paths []
I already changed the the code but still not working
// dd($this->gallery_image);
$action = '';
$data = $this->validate([
'gallery_image.*' => 'image|max:1024',
]);
if(!empty($this->gallery_image)){
$gallery_image = 'gallery_image.' . time() . $this->gallery_image->getClientOriginalName();
foreach( $this->gallery_image as $images ){
$images->storeAs('public/slides-images', $gallery_image);
}
$data['gallery_image'] = $gallery_image;
}
if($this->galleryId){
Gallery::find($this->galleryId)->update($data);
$action = 'edit';
}else{
Gallery::create($data);
$action = 'store';
}
$this->emit('showEmitedFlashMessage', $action);
$this->resetInputFields();
$this->emit('refreshParent');
$this->emit('closeGalleryModal');
}
}
What do you expect $data will contain when you pass it to the Gallery model update or create method?
I want to save the name to the database and display the images in my gallery section in my front-end. This where I get the name
$gallery_image = 'gallery_image.' . time() . $this->gallery_image->getClientOriginalName();
Your Gallery object. Is it for a single image or do you have some way of storing the names of multiple images in one model?
You are demonstrating lack of understanding of some of the basics.
To save the array of images' paths:
change $data['gallery_image'] = $gallery_image; to
$data['gallery_image'] = $paths;
Is this the right code? I check the storage folder doesn't save the images and in the database
if (!empty($this->gallery_image)) {
$images = $this->gallery_image;
$paths = [];
foreach ($images as $image) {
$paths[] = Storage::putFileAs('public/slides-images', $image, 'gallery_image_' . time() . $image->getClientOriginalName());
}
$data['gallery_image'] = $paths;
}
I tried to follow your video but still doesn't work on me
public function store(){
$action = '';
$data = $this->validate([
'gallery_image.*' => 'image|max:1024',
]);
if(!empty($this->gallery_image)){
$gallery_image = 'gallery_image.' . time() . $this->gallery_image->getClientOriginalName();
$path = [];
foreach( $this->gallery_image as $images){
$path = $images->storeAs('public/gallery', $gallery_image);
}
$data['gallery_image'] = $path;
}
if($this->gallery_imageId){
Banner::find($this->bannerId)->update($data);
$action = 'edit';
}else{
Banner::create($data);
$action = 'store';
}
$this->emit('showEmitedFlashMessage', $action);
$this->resetInputFields();
$this->emit('refreshParent');
$this->emit('closeBannerModal');
}
Any Idea how to revised the code?
As mentioned by snapey: You are demonstrating lack of understanding of some of the basics. . Try to learn the basics first, I guess?
The function getClientOriginalName() is works on a single file object, not a file array.
Note: $this->gallery_image is an array
if (!empty($this->gallery_image)) {
//I prefer to name it as $paths with 's' because plural means more than one
$paths = [];
//Same thing here, $image means it is a single file object
foreach ($this->gallery_image as $image) {
$paths[] = $image->storeAs('public/gallery', 'gallery_image.' . time() . $image->getClientOriginalName());
dd("hello world", $paths);
//I add dd() here just to debug. Make sure the for loop is ran and $paths is returned.
}
$data['gallery_image'] = $paths;
}
still doesn't working... :(
Please define doesn't work. Thanks.
member for two years, completed lessons 3. At some point you have to take responsibility for your own learning
Thanks for the encouragement sir.. I solved the problem
my new code
public function store(){
$action = '';
$data = $this->validate([
'album_title' => 'required',
'photos.*' => 'image|max:2000', // 2MB Max
]);
//New Code with collections
$filenames = collect($this->photos)->map->store('photos');
if($this->galleryId){
Gallery::find($this->galleryId)->update($data);
$action = 'edit';
}else{
// Gallery::create($data);
Gallery::create([
'album_title' => $this->album_title,
'photos' => $filenames->implode(','),
]);
$action = 'store';
}
$this->emit('showEmitedFlashMessage', $action);
$this->resetInputFields();
$this->emit('refreshParent');
$this->emit('closeGalleryModal');
}
Please sign in or create an account to participate in this conversation.