Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

javascript - How to insert and upload multiple file using laravel?

<h4 class="card-title">Add Product</h4>
<form class="forms-sample" action="{{route('AddNewProduct')}}" method="post" enctype="multipart/form-data">
    {{csrf_field()}}
    <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>
</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

question from:https://stackoverflow.com/questions/65931164/how-to-insert-and-upload-multiple-file-using-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the code below:

public function AddNewProduct(Request $request)
{
    $date=date('d-m-Y');

    $this->validate(

        $request, [
                    'product_image' => 'required',
                    'product_image.*' => 'mimes:jpeg,png,jpg',
                ]
        );

    $temImageArray = [];

    $product_image = [];

    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; 

            array_push( $temImageArray ,  $product_image);
        }
    }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...