如何使用laravel在数据库中上传多个文件

Posted

技术标签:

【中文标题】如何使用laravel在数据库中上传多个文件【英文标题】:How to upload multiple files in database using laravel 【发布时间】:2021-12-16 18:09:25 【问题描述】:

我是 laravel 的新手。我正在尝试以表格形式上传多个文件,但是当我尝试插入数据时,它显示错误。我需要将文件名保存到数据库中。每当我尝试保存表单时,它都会显示错误,因为文件不能为空。

我试过的是:

树枝:

 <form method="POST" action="/listingSave" enctype="multipart/form-data" class="sellingFormSave" id="sellingFormSave">
                                        <input type="hidden" name="_token" value="csrf_token()"/>
                                        <input type="hidden" name="product_id" value="product.id"/>
                                        <div class="row">
                                            <div class="form-group col-md-12 condition_radio">
                                                <div class="wrapper">
                                                    <span>Condition of the Trucks</span>
                                                    <div class="toggle_radio">
                                                        <input type="radio" class="toggle_option" id="as_is" value="as_is" name="condition" product.condition == 'as_is' ? 'checked':'' product != '' ? 'disabled' : ''>
                                                        <input type="radio" class="toggle_option" id="dot_term" value="dot" name="condition" product.condition == 'dot' ? 'checked':'' product != '' ? 'disabled' : ''>
                                                        <input type="radio" class="toggle_option" id="trade_term" value="trade" name="condition" product.condition == 'trade' ? 'checked':'' product != '' ? 'disabled' : ''>
                                                        <label for="as_is"><p>As Is - Where Is</p></label>
                                                        <label for="dot_term"><p>DOT Term</p></label>
                                                        <label for="trade_term"><p>Trade Term</p></label>
                                                        <div class="toggle_option_slider">
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-12">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Name *" name="name" value="product.name" product != '' ? 'disabled' : 'required'>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row sell_form">
                                            <div class="col-12 section_title-js">
                                                <h3>Location of the Trucks</h3>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="City *" name="city" value="product.city" product != '' ? 'disabled' : 'required'>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                     form.fields.state.setAttributes( 'required' : 'required' ).input|raw 
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Zip *" name="zipcode" value="product.zipcode" product != '' ? 'disabled' : 'required'>
                                                </div>
                                            </div>
                                        </div>
                                        % if product == '' %
                                        <div class="row">
                                            <div class="col-12 section_title-js">
                                                <h3></h3>
                                            </div>
                                            <div class="form-group col-md-12">
                                                <input id="demo" type="file" name="files" accept=".jpg, .png, image/jpeg, image/png" multiple>
                                                <!-- <input type="hidden" name="image[]" accept=".jpg, .png, image/jpeg, image/png" multiple> -->
                                            </div>
                                        </div>
                                        % endif %
                                        <div class="row">                                       
                                            <div class="form-group col-md-12 col-12">
                                                <div class="field__input-wrapper">
                                                    <input type="radio" name="quantity_type" value="single" product != '' ? 'disabled' : 'required' product.quantity_type == 'single' ? 'checked' : ''> Single
                                                    <input type="radio" name="quantity_type" value="fleet" product != '' ? 'disabled' : 'required' product.quantity_type == 'fleet' ? 'checked' : ''> Fleet
                                                </div><br>
                                                <!-- <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Quantity *" name="quantity_on_hand" 
                                                           value="product.quantity_on_hand" product != '' ? 'readonly' : 'required'>
                                                </div><br>
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Price($)" name="price" 
                                                           value="product.price" product != '' ? 'readonly' : ''>
                                                </div> -->
                                            </div>
                                        </div>
                                        
                                        <div class="row">   
                                            <div class="form-group col-md-12 col-12">
                                                <table class="table mileageTable">
                                                    <thead>
                                                        <tr>
                                                            <th>Mileage</th>
                                                            <th>Price</th>
                                                            <th>Count</th>
                                                            % if product != '' %
                                                            <th>Sold</th>
                                                            % endif %
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        % for mile in miles %
                                                        <tr>
                                                            <td>
                                                                <input type="hidden" name="mileage_gap_id[]" value="mile.id">
                                                                mile.name
                                                            </td>
                                                            <td>
                                                                <input class="price" name="mileage_price[mile.id]" 
                                                                       value="product.mileage_gaps.where('mileage_gap_id',mile.id).first() ? product.mileage_gaps.where('mileage_gap_id',mile.id).first().price : ''"
                                                                      >                                                         </td>   
                                                            <td>
                                                                <input class="number_of_products" name="number_of_products[mile.id]" 
                                                                       value="product.mileage_gaps.where('mileage_gap_id',mile.id).first() ? product.mileage_gaps.where('mileage_gap_id',mile.id).first().number_of_products : ''"
                                                                       >
                                                            </td>   
                                                            % if product != '' %      
                                                            <td>
                                                                <input class="number_of_products_sold" name="number_of_products_sold[mile.id]" 
                                                                       value="product.mileage_gaps.where('mileage_gap_id',mile.id).first() ? product.mileage_gaps.where('mileage_gap_id',mile.id).first().number_of_products_sold : ''"
                                                                       type="number">
                                                            </td>               
                                                            % endif %                             
                                                        </tr>
                                                        % endfor %
                                                    </tbody>
                                                </table>
                                            </div>  
                                            
                                        </div>
                                        % if product != '' %
                                        <div class="row">
                                            <div class="form-group col-md-4">
                                                <span>Status</span>
                                                <select class="form-control" name="status">
                                                    <option value="open" product.status == 'open' ? 'selected' : ''>Open</option>
                                                    <option value="pending" product.status == 'pending' ? 'selected' : ''>Pending</option>
                                                    <option value="sold" product.status == 'sold' ? 'selected' : ''>Sold</option>
                                                </select>
                                            </div>
                                        </div>
                                        % endif %

                                        <div class="row">
                                            <div class="col-12 section_title-js">
                                                <h3></h3>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Engine Type" name="engine_type" value="product.engine_type" product != '' ? 'disabled' : ''>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Horsepower" name="horsepower" value="product.horsepower" product != '' ? 'disabled' : ''>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <select name="transmission" class="field__input" data-field="transmission" data-field_name="transmission" 
                                                            data-provides="anomaly.field_type.transmission" id="transmission" product != '' ? 'disabled' : ''>
                                                        <option value="">Type of Transmission</option>
                                                        <option value="Automated" product.transmission == 'Automated' ? 'selected' : ''>Automated</option>
                                                        <option value="Automatic" product.transmission == 'Automatic' ? 'selected' : ''>Automatic</option>
                                                        <option value="Manual" product.transmission == 'Manual' ? 'selected' : ''>Manual</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Transmission Speed" name="transmission_speed" value="product.transmission_speed" product != '' ? 'disabled' : ''>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <input type="text" class="field__input" placeholder="Transmission Model" name="transmission_model" value="product.transmission_model" product != '' ? 'disabled' : ''>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <div class="field__input-wrapper">
                                                    <select name="bunks" class="field__input" data-field="bunks" 
                                                            data-field_name="bunks" id="bunks" product != '' ? 'disabled' : ''>
                                                        <option value=""># of Bunks</option>
                                                        <option value="1" product.bunks == '1' ? 'selected' : ''>1</option>
                                                        <option value="2" product.bunks == '2' ? 'selected' : ''>2</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-12">
                                                <div class="field__input-wrapper">
                                                    <textarea placeholder="Description of the Trucks" class="form-control" id="description" name="description" rows="3" product != '' ? 'disabled' : ''>product.description</textarea>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-12 section_title-js">
                                                <h3></h3>
                                            </div>
                                            <div class="form-group col-md-12">
                                                <div class="field__input-wrapper field_input__confirm">
                                                    <input type="checkbox" id="confirm" name="confirm[]" value="yes" product.confirm != '' ? 'checked' : '' product != '' ? 'disabled' : 'required'>
                                                    <label for="confirm"> I certify that the above facts are true to the best of my knowledge and belief.</label>
                                                </div>
                                            </div>
                                        </div>
                                        <button type="submit" class="btn">
                                            Save
                                        </button>
                                        <a href="/truckian-dashboard" class="_btn dash_btn">Back to Dashboard</a>

                                    </form>

控制器:

public function listingSave(Request $request) 
    
    $condition=$request->input('condition');
    echo $condition;
    $p_name=$request->input('name');
    $city=$request->input('city');
    $zipcode=$request->input('zipcode');  
    $qtype=$request->input('quantity_type');
    $status=$request->input('status');
    $engine_type=$request->input('engine_type');
    $horsepower=$request->input('horsepower');
    $transmission=$request->input('transmission');
    $transmission_model=$request->input('transmission_model');
    $transmission_speed=$request->input('transmission_speed');
    $description=$request->input('description');
     $confirm=$request->input('confirm[]');
      $bunks=$request->input('bunks');
   $files=$request->input('files');
    DB::insert('insert into default_truckian_products(product_name,p_condition,city,zipcode,files,quantity_type,status,engine_type,horse_power,transmission, transmission_speed,transmission_model, bunks,description,confirm) values (?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?)', [$p_name,$condition,$city,$zipcode,$files,$qtype,$status,$engine_type,$horsepower,$transmission,$transmission_speed,$transmission_model,$bunks,$description,$confirm]);

当尝试执行此代码时,它会显示如下错误:

如何将文件名和文件插入数据库。

【问题讨论】:

您的文件变量是 $file=$request->input('files'); 但在您的查询中您使用的是 $files (没有价值,所以它是空的)。您必须将分配修改为 $files=$request->input('files'); @nacho 。不工作 查看 laravel 文档:laravel.com/docs/8.x/filesystem#file-uploads 你应该使用 $request->file('files') 来访问上传的文件。 【参考方案1】:
 <input id="demo" type="file" name="files[]" accept=".jpg, .png, image/jpeg, image/png" multiple>

        if($request->hasfile('files'))
         
            foreach($request->file('files') as $file)
            
                $name = time().'.'.$file->extension();
                $file->move(public_path().'/files/', $name);  
                $data[] = $name;  
            
         

试试这个。它对我有用。

【讨论】:

以上是关于如何使用laravel在数据库中上传多个文件的主要内容,如果未能解决你的问题,请参考以下文章

如何防止在laravel中上传相同的文件

如何从 Laravel Livewire 的属性中删除上传的文件?

如何在 Vue 和 Laravel 中使用 axios 上传图像文件和数据对象?

如何克服 Laravel dropzone 文件上传中的 getClientOriginalName() 错误?

如何在 laravel 上上传文件并将其存储在目录中

如何使用 xampp 在本地运行多个 laravel 应用程序并使它们可供我本地网络上的其他 PC 访问