如何在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'); } }
表格是-
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())@endif @foreach($cars as $car)@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach{{ $car->your-fields-from-cars-table }}
//and get all relevant images for car by this @foreach($car->carevidence as $evidence) @endforeach @endforeach
我希望这能帮到您!
我认为,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'); } }
表格是-
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())@endif @foreach($cars as $car)@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach{{ $car->your-fields-from-cars-table }}
//and get all relevant images for car by this @foreach($car->carevidence as $evidence) @endforeach @endforeach
我希望这能帮到您!