gpt4 book ai didi

php - 上传不同属性的多个文件

转载 作者:行者123 更新时间:2023-12-02 06:26:49 26 4
gpt4 key购买 nike

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

浏览量

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

Controller

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

最佳答案

我认为,不同的属性,如 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');

然后在您的 Controller 方法中-

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

最后,在返回 Blade 文件期间

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

希望对您有所帮助!

关于php - 上传不同属性的多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58076087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com