Reputation: 240
How to upload multiple selected files in different attribute such as pic_1,pic_2,pic_3 in MYSQL
Views
<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");
Upvotes: 2
Views: 894
Reputation: 3751
I think, different attribute such as pic_1,pic_2,pic_3
for your cars
single table make you trouble to handle files. My suggestion is, do it usingrelationship
. If so, you are storing some information about cars into your cars
table. And each car may have multiple images for showing as car evidence. In this situation, It would be better you make another table such as car_evidence
make two columns in this table named cars_id
and car_images
. Then make a relationship between them. If you do like this, then you can submit more car images as you want dynamically. pic_1,pic_2,pic_3 attributes not be better way. Please, have a look to my process -
Your Cars.php model -
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');
}
}
Make a new model CarEvidence
with migration by running php artisan make:model CarEvidence -m
. Then, inside this migration file just add two columns
like this -
$table->bigInteger('cars_id')->unsigned()->nullable();
$table->string('car_images');
Your CarEvidence.php model should look like-
class CarEvidence extends Model
{
protected $fillable = [
'cars_id', 'car_images',
];
public function car(){
return $this->belongsTo('App\Cars', 'cars_id', 'id');
}
}
The form is -
<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');
And then in your controller method-
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");
}
And, finally during return your blade file
public function yourmethodname()
{
$cars = Cars::with('carevidence')->get();
return view('your-view-blade-file', compact('cars'));
}
Then inside your-view-blade-file
to get the car images(evidence) you can use nested foreach
loop -
@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
I hope this will help you!
Upvotes: 7
Reputation: 243
follow like this Code :
in View file
- <input type="file" name="carEvidence[]" multiple>
Upvotes: 1
Reputation: 1673
In addition to @ollieread
's answer, You can loop through the array to store the image or any other uploded file in database or anywhere .
For getting files as multiple,
<input type = "file" name = "carEvidence[]" multiple>
Then in your controller file , use the for loop
to get the every single files .
$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();
Simple. Try it and let me know .
Upvotes: 1
Reputation: 6291
Simply providing the multiple
attribute for elements such as this isn't everything you need. When making a multiple-input type, whether it's file
, select
or even input
, you need to suffix the name with []
, so in your example, the field would be:
<input type="file" name="carEvidence[]" multiple>
From there you can cycle through them and upload them appropriately.
Upvotes: 1