Your question is not clear. Nobody will be able to help you in this case.
Feb 14, 2021
9
Level 4
Upload Multiple Images with multidimensional array
Hello
Tying create Upload Multiple Images with multidimensional array
DB
Schema::create('galleries', function (Blueprint $table) {
$table->id();
$table->string('category')->nullable();
$table->string('image')->nullable();
$table->timestamps();
});
<input type="file" id="image" name="image[]" class="form-control" multiple>
controller
$path = "upload/";
foreach ($image as $multi_img) {
$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
$last_img = $path.$name_gen;
Gallery::insert([
'image'=> $last_img,
]);
}
This works and it uploads image.
But I want make like
<input type="file" id="image" name="image[1][]" class="form-control" multiple>
<input type="file" id="image" name="image[2][]" class="form-control" multiple>
<input type="file" id="image" name="image[3][]" class="form-control" multiple>
How make for each for this image[X][]
If file uploaded from image[1][] in 'category'=> 1
$path = "upload/";
foreach ($image as $multi_img) {
$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
$last_img = $path.$name_gen;
Gallery::insert([
'image'=> $last_img,
'category'=> ???,
]);
}
Level 5
because image not are array of image now, its array of category. try to:
$request->validate([
'image' => ['required', 'array'],
'image.*.*' => 'mimes:jpeg,jpg,png,gif|max:2048'
]);
$path = "upload/";
foreach ($request->input('image') AS $category => $image) {
foreach ($request->file('image')[$category] as $multi_img) {
$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
$last_img = $path.$name_gen;
Gallery::insert([
'image'=> $last_img,
'category' => $category
]);
}
}
1 like
Please or to participate in this conversation.