如何在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");
答案 0 :(得分: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');
}
}
表格为-
<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
希望这对您有帮助!
答案 1 :(得分:1)
仅为诸如此类的元素提供multiple
属性并不是您所需要的。在创建多输入类型时,无论是file
,select
还是input
,都需要在名称后加上[]
,因此在您的示例中,该字段将是:
<input type="file" name="carEvidence[]" multiple>
您可以从那里循环浏览并适当地上传它们。
答案 2 :(得分:1)
除了@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();
简单。试试看,让我知道。
答案 3 :(得分:1)
遵循以下代码:
在查看文件中
-<input type="file" name="carEvidence[]" multiple>