Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

plue's avatar
Level 1

Upload multiple image and insert in multiple row in database

I want to upload multiple image but I want to insert them into multiple rows. For example i upload 5 images and it will store in the database with 5 rows. I dont know how to do it in controller. Can somebody help me? Here is the blade:

<form method="post" action="create-photos" enctype="multipart/form-data">
            @csrf
            <div class="form-group">
                <label>Title</label>
                <input type="text" class="form-control" name="title" ><br>
                <label for="exampleFormControlFile1">Upload Photos</label><br>
                <input type="file" class="form-control-file" name="image[]" multiple id="exampleFormControlFile1" required><br><br>
            </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Post</button>
      </div>
      </form>
0 likes
14 replies
MohamedTammam's avatar

In your controller methods

$request->validate([
	'image' => 'required|array',
	'image.*' => 'file',
]);

// Looping all files
foreach($request->image as $img) {

	$filePath = $img->store('your_public_path', 'public');
	// Store it in your database
	// ...
}

And of course you need to do error handling whenever it's needed according to your case.

plue's avatar
Level 1

update: i made a progress, I can upload multiple image now but the problem is I cant insert the title correctly, I tried to do it but it insert in another row. How can I insert it with the image? Here is my controller:

public function store(Request $request) {
       
        if($files = $request->file('image')) {
        $filesCollection = collect([]);
        foreach ($files as $file) {
            $title = $request->title;
        $path = $file->storeAs('featured-photos', md5(rand(1000, 10000)). '.' . $file->getClientOriginalExtension());
        $file->move('featured-photos', $path);
        $filesCollection->push($path, $title);
     
    }
}

    $filesCollection->each(function ($path, $title) {
             Photo::create([
                         'image' => $path,
						'title' => $title,
                     ]);
        });
     return redirect()->back();

    }

Sinnbeck's avatar

@plue so you could just do

$title = $request->title;
$filesCollection->each(function ($path) use ($title) {
             Photo::create([
                         'image' => $path,
						'title' => $title,
                     ]);
        });
1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Or just do it in the original loop

if($files = $request->file('image')) {
        $title = $request->title;
        foreach ($files as $file) {
            
        $path = $file->storeAs('featured-photos', md5(rand(1000, 10000)). '.' . $file->getClientOriginalExtension());
        $file->move('featured-photos', $path);
        Photo::create([
                         'image' => $path,
						'title' => $title,
                     ]);
     
    }
} 
2 likes
plue's avatar
Level 1

@Sinnbeck I tried both but it didn't insert the title into the database it shows null, I tried to dump the title but it show the title that I input

plue's avatar
Level 1

@Snapey ow shoot I forgot I removed it earlier! Thanks it's working now!

Sinnbeck's avatar

@plue great. Please pick a best answer to set the thread as solved

Snapey's avatar
public function store(Request $request)
{
    if($files = $request->file('image')) {
        foreach ($files as $file)
        {
            $path = $file->storeAs('featured-photos', md5(rand(1000, 10000)). '.' . $file->getClientOriginalExtension());
            $file->move('featured-photos', $path);
            Photo::create([
                'image' => $path,
		        'title' => $request->title,
            ]);     
        }
    }
    return redirect()->back();
 }

I would have a much larger random range for your image as with just 5 digits a collision will happen remarkably quickly

1 like
plue's avatar
Level 1

@Snapey i got the same output, the title is null. I checked my input name in the html and it's correct

Please or to participate in this conversation.