首页 > 解决方案 > 如何将图像的原始名称放入数组中?

问题描述

大家好,我尝试上传两张图片,但是当我dd 使用 $filename 时,我在数组中只有一个名称

$data =  $this->validate([
            'image.*' => 'image|max:1024', // 1MB Max
        ]);

if(!empty($this->image)){

$filename = [];

foreach ($this->image as $img) {

    $filename =  $img->storeAs('public/images', 'image_' . time() . $img->getClientOriginalName());                        
    
    
}

$data['image'] = $filename;

dd($filename); 

}

因为当dd我创建它的数据时,它只传递一个名称

if($this->galleryId){
    Gallery::find($this->galleryId)->update($data);
    $action = 'edit';
}else{
    dd($data);
    Gallery::create($data);
    $action = 'store';
}

这是我的数据dd

"public/images/image_1628844415Facebook-0064.jpg"

标签: laravellaravel-livewire

解决方案


承格这条线

$filename =  $img->storeAs('public/images', 'image_' . time() . $img->getClientOriginalName());

$filename[] =  $img->storeAs('public/images', 'image_' . time() . $img->getClientOriginalName());

推荐阅读