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>
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.
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();
}
@plue so all images has the same title? $request->title
@plue so you could just do
$title = $request->title;
$filesCollection->each(function ($path) use ($title) {
Photo::create([
'image' => $path,
'title' => $title,
]);
});
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,
]);
}
}
@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 if your title $fillable in the model?
@Snapey ow shoot I forgot I removed it earlier! Thanks it's working now!
@plue great. Please pick a best answer to set the thread as solved
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
@Snapey i got the same output, the title is null. I checked my input name in the html and it's correct
Please sign in or create an account to participate in this conversation.