mercredi 27 janvier 2021

How to insert and upload multiple file using laravel?

<h4 class="card-title">Add Product</h4>
<form class="forms-sample" action="" method="post" enctype="multipart/form-data">
    
    <div id="dynamicTable">
        <div class="row">
            <div class="col-md-4">
                <div class="form">
                    <label class="bmd-label-floating">Product Image</label>
                    <input type="file" name="product_image[]" class="form-control">
                </div>
            </div>
            <div class="col-md-2">
                <div class="form">
                    <button type="button" name="add" id="add" class="btn btn-success"><i class="fa fa-plus"></i></button>
                </div>
            </div>
        </div>
    </div>
    <button type="submit" class="btn btn-primary pull-center">Submit</button>
    <a href="" class="btn">Close</a>
    <div class="clearfix"></div>
</form>

<script type="text/javascript">
    var i = 0;
    $("#add").click(function(){
        ++i;
        $("#dynamicTable").append('<div class="row"><div class="col-md-4"><div class="form"><input type="file" name="product_image[]" class="form-control"></div></div><div class="col-md-2"><div class="form"><button type="button" class="btn btn-danger remove-tr"><i class="fa fa-minus"></i></button></div></div></div>');
    });
    $(document).on('click', '.remove-tr', function(){  
        $(this).closest(".row").remove();
    });  
</script>

function:

public function AddNewProduct(Request $request)
{
    $date=date('d-m-Y');
    $this->validate(
        $request, [
                    'product_image' => 'required',
                    'product_image.*' => 'mimes:jpeg,png,jpg',
                ]
        );

    if($request->hasfile('product_image'))
    {
        foreach($request->file('product_image') as $file)
        {
            $fileName = $file->getClientOriginalName();
            $fileName = str_replace(" ", "-", $fileName);
            $file->move('images/product/'.$date.'/', $fileName);
            $product_image[] = 'images/product/'.$date.'/'.$fileName; 
        }
    }

    echo "<pre>";
    print_r($product_image);
}

In the above code I am add multiple file dynamically via click on add button which is perfectly working now what happen when I click on submit button and I am trying to print multiple image then only 1 file is showing I don't now why? So, How can I upload multiple file and insert into database using laravel? Please help me.

Thank You



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3qUCRzZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire