0

I want to upload multiple images using 2 input file fields in Laravel and put that 2 files to DB with different attributes (imagepath1, imagepath2). If I try that code there both input & upload the same file like imagekitchen2 (there both change but imagepath1 become imagepath2 and imagepath2 still imagepath2).

Controller

public function store(Request $request)
{
    $kitchens = new Kitchen();
    $kitchens->title = $request->input('title-kitchen');
    $kitchens->description = $request->input('description-kitchen');
    if ($request->hasfile('imagekitchen1')) {
        $file = $request->file('imagekitchen1');
        $extension = $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file->move('uploads/product/kitchen/', $filename);
        $kitchens->imagepath1 = $filename;
    } else {
        $kitchens->imagepath1 = '';
    }

    $kitchens->save();
    if ($request->hasfile('imagekitchen2')) {
        $file = $request->file('imagekitchen2');
        $extension = $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file->move('uploads/product/kitchen/', $filename);
        $kitchens->imagepath2 = $filename;
    } else {
        $kitchens->imagepath2 = '';
    }

    $kitchens->save();
}

View

<div class="card-body">
    <div class="row">
        <div class="col-md-6">
            <form action="{{ route('addimagekitchen') }}" enctype="multipart/form-data" method="POST">
                {{ csrf_field() }}
                <div class="form-group">
                    <label>Title</label>
                    <label>
                        <input type="text" name="title-kitchen" class="form-control">
                    </label>
                </div>
                <div class="input-group">
                    <div class="custom-file">
                        <label for="image" style="display: block">Main image</label> <br/>
                        <input type="file" name="imagekitchen1" style="margin-left: 20px">
                    </div>
                </div>
                <div class="input-group">
                    <div class="custom-file">
                        <label for="image" style="display: block">Second image</label> <br/>
                        <input type="file" name="imagekitchen2" style="margin-left: 20px">
                    </div>
                </div>
                <div class="input-group">
                    <div class="custom-file">
                        <label for="image" style="display: block">Third image</label> <br/>
                        <input type="file" name="imagekitchen[]" style="margin-left: 20px">
                    </div>
                </div>
                <div class="form-group">
                    <label>Description</label>
                    <textarea class="form-control" name="description-kitchen" id="description-kitchen"
                              rows="3"></textarea>
                </div>
                <button type="submit" class="btn btn-success"> Insert</button>
                <a href="/kitchen-admin" class="btn btn-danger"> Cancel </a>
            </form>
        </div>
    </div>
</div>
Karl Hill
  • 12,937
  • 5
  • 58
  • 95

1 Answers1

0

I'm not completely sure what you mean, but probably you should change:

$kitchens->save();
if($request->hasfile('imagekitchen2')){

into

$kitchens->save();

$kitchens = new Kitchen(); 
if($request->hasfile('imagekitchen2')){

this way, you will create 2 records, otherwise you used same object and updated it after creation. Depending on your needs you might also want to add:

$kitchens->title = $request->input('title-kitchen');
$kitchens->description = $request->input('description-kitchen');

before:

if($request->hasfile('imagekitchen2')){

in case you want to save same title and description for both records.

Of course I'm not sure if you want to create records if there are no files - at the moment both will be saved in case no file uploaded.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291