当前位置:  开发笔记 > 编程语言 > 正文

上载具有不同属性的多个文件

如何解决《上载具有不同属性的多个文件》经验,为你挑选了1个好方法。

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

观看次数

 

控制者

  $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");

Rashed Hasan.. 5

我认为,different attribute such as pic_1,pic_2,pic_3对于您的cars单个表,您将很难处理文件。我的建议是使用relationship。如果是这样,则您将有关汽车的一些信息存储到cars表中。每辆汽车可能有多个图像作为汽车证据显示。在这种情况下,最好制作另一个表,例如car_evidence在该表中命名为cars_id和的两列car_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');
    }
}

CarEvidence通过运行迁移来创建新模型php artisan make:model CarEvidence -m。然后,在此迁移文件中只需添加两个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');
    }
}

表格是-

@csrf //your other input fields about car information here, I think if any

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'))
    
{{ session('success') }}
@endif @if ($errors->any())
    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif @foreach($cars as $car)

{{ $car->your-fields-from-cars-table }}

//and get all relevant images for car by this @foreach($car->carevidence as $evidence) @endforeach @endforeach

我希望这能帮到您!



1> Rashed Hasan..:

我认为,different attribute such as pic_1,pic_2,pic_3对于您的cars单个表,您将很难处理文件。我的建议是使用relationship。如果是这样,则您将有关汽车的一些信息存储到cars表中。每辆汽车可能有多个图像作为汽车证据显示。在这种情况下,最好制作另一个表,例如car_evidence在该表中命名为cars_id和的两列car_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');
    }
}

CarEvidence通过运行迁移来创建新模型php artisan make:model CarEvidence -m。然后,在此迁移文件中只需添加两个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');
    }
}

表格是-

@csrf //your other input fields about car information here, I think if any

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'))
    
{{ session('success') }}
@endif @if ($errors->any())
    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif @foreach($cars as $car)

{{ $car->your-fields-from-cars-table }}

//and get all relevant images for car by this @foreach($car->carevidence as $evidence) @endforeach @endforeach

我希望这能帮到您!

推荐阅读
勤奋的瞌睡猪_715
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有