【问题标题】:Upload Multiple File In Different Attribute上传不同属性的多个文件
【发布时间】:2019-09-24 08:28:13
【问题描述】:

如何在MYSQL中上传多个不同属性的选定文件,例如pic_1,pic_2,pic_3

观看次数

 <input type="file" name="carEvidence" multiple>

控制器

  $this->validate($request, [
            'carEvidence' =>  'required|mimes:jpeg,png,jpg,zip,pdf|max:2048',
        ]);
 $post = new Cars;
 if ($files = $request->file('carEvidence')) {
            $destinationPath = 'public/image/'; // upload path
            $profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
            $files->move($destinationPath, $profileImage);
            $post['carEvidence'] = "$profileImage";
        }
$post->save();
        return redirect('/internalaudit')->with('success', "Has been sent for Validation");

【问题讨论】:

  • 您的输入 type="file" 需要名称为 carEvidence[] 才能接受多个文件作为数组。然后使用foreach循环遍历数组,然后一个一个存储
  • 如果这个问题解决了,我会接受答案! :)
  • @Steven 属性表示不同的数据库列。像 pic_1,pic_2,pic_3 或其他..
  • @AmitSenjaliya 是的兄弟

标签: php laravel file-upload laravel-5.8


【解决方案1】:

我认为,different attribute such as pic_1,pic_2,pic_3 为你的cars 单表让你处理文件很麻烦。我的建议是,使用relationship。如果是这样,您将一些关于汽车的信息存储到您的cars 表中。每辆车可能有多个图像作为汽车证据显示。在这种情况下,最好创建另一个表,例如car_evidence,在此表中创建两个名为cars_idcar_images 的列。然后建立他们之间的关系。如果您这样做,那么您可以根据需要动态提交更多汽车图像。 pic_1,pic_2,pic_3 属性不是更好的方法。请看一下我的过程- 你的 Cars.php 模型 -

class Cars extends Model
{
    protected $primaryKey = 'cars_id';

    protected $fillable = [
        //your cars table's other fields
    ];

    public function carevidence()
    {
        return $this->hasMany('App\CarEvidence', 'cars_id', 'id');
    }
}

通过运行php artisan make:model CarEvidence -m 迁移创建一个新模型CarEvidence。然后,在这个迁移文件中,像这样添加两个columns -

$table->bigInteger('cars_id')->unsigned()->nullable();
$table->string('car_images');

你的 CarEvidence.php 模型应该看起来像 -

class CarEvidence extends Model
{
    protected $fillable = [
        'cars_id', 'car_images',
    ];

    public function car(){
        return $this->belongsTo('App\Cars', 'cars_id', 'id');
    }
}

形式是-

<form action="your-route-or-url-here" method="post" enctype="multipart/form-data">
    @csrf
    //your other input fields about car information here, I think if any
    <input type="file" name="carEvidence[]" multiple>
    <button type="submit">Save</button>
</form>

Web.php

Route::post('/your-form-action-url-here', 'YourControllerHere@YourMethodHere');

然后在你的控制器方法中-

public function YourMethodHere(Request $request){
    $this->validate($request, [
             'carEvidence.*' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ]);

     $post = new Cars;
     //$post->carEvidence = "yes";here other information about car to save in `cars` table
     if ($post->save()) {
          if($request->hasFile('carEvidence')){
             $files = $request->file('carEvidence');
             foreach($files as $file){
                 $extension = $file->getClientOriginalExtension();
                 $filename =time().'.'.$extension;
                 $file->move(public_path('/image'), $filename);
                 CarEvidence::create([
                    'cars_id' => $post->id,
                    'car_images' => $filename
                 ]);
              }
          }
      }

  return redirect('/internalaudit')->with('success', "Has been sent for Validation");
}

最后,在返回刀片文件期间

public function yourmethodname()
{
$cars = Cars::with('carevidence')->get();
return view('your-view-blade-file', compact('cars'));
}

然后在your-view-blade-file 中获取汽车图像(证据),您可以使用嵌套的foreach 循环-

@if (session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif
@if ($errors->any())
    <div class="alert alert-danger">
         <ul>
             @foreach ($errors->all() as $error)
             <li>{{ $error }}</li>
             @endforeach
         </ul>
    </div>
@endif


@foreach($cars as $car)
    <p>{{ $car->your-fields-from-cars-table }}</p>
          //and get all relevant images for car by this
          @foreach($car->carevidence as $evidence)
            <img src="{{ asset('image/'.$evidence->car_images) }}" height="60px" width="60px" />
          @endforeach
@endforeach

希望对你有帮助!

【讨论】:

  • 好吧,我试着告诉你:) 谢谢!
  • 谢谢兄弟,它有效! :) 将在 5 小时内奖励赏金!
  • 我很高兴,它对你有用。不客气。 :)
【解决方案2】:

仅仅为诸如此类的元素提供multiple 属性并不是您所需要的一切。在制作多输入类型时,无论是fileselect 还是input,都需要在名称后加上[],因此在您的示例中,该字段为:

<input type="file" name="carEvidence[]" multiple>

您可以从那里循环浏览它们并适当地上传它们。

【讨论】:

  • 那就是name="carEvidence[]"
【解决方案3】:

除了@ollieread 的回答之外,您还可以遍历数组以将图像或任何其他上传的文件存储在数据库或任何地方。 要获取多个文件,

<input type = "file" name = "carEvidence[]" multiple>

然后在您的控制器文件中,使用for loop 获取每个文件。

 $post = new Cars;
 if ($files = $request->file('carEvidence')) {
    foreach($request->file('carEvidence) as $file) {
        $destinationPath = 'public/image/'; // upload path
        $profileImage = date('YmdHis') . "." . $file->getClientOriginalExtension();
        $files->move($destinationPath, $profileImage);
        $post['carEvidence'] = "$profileImage";
    }
}
$post->save();

简单。试试看,让我知道。

【讨论】:

    【解决方案4】:

    按照这样的代码: 在查看文件中 - &lt;input type="file" name="carEvidence[]" multiple&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多