$request->file will be a collection;
$files = $request->file('vehimage');
foreach($files as $file){
//process each image
}
Hello. Basically I have a form that stores some information and an image in a MySql database. However, I have to change this to taking up to multiple images. I've tried looping through them with @foreach but can't get it working. Some help would be appreciated. Here is my code:
VehiclesModel.php
class VehiclesModel extends Model
{
protected $table = 'vehs' ;
protected $fillable = [
'vehmod', 'vehmark', 'vehyear', 'vehengine', 'vehtrans', 'vehimage'
] ;
}
VehiclesController.php
use App\VehiclesModel ;
public function store(Request $request)
{
$vehiclesmodel = new VehiclesModel ;
$vehiclesmodel->vehmod = $request->vehmod ;
$vehiclesmodel->vehmark = $request->vehmark ;
$vehiclesmodel->vehyear = $request->vehyear ;
$vehiclesmodel->vehengine = $request->vehengine ;
$vehiclesmodel->vehtrans = $request->vehtrans ;
$vehiclesmodel->vehimage = $request->vehimage ;
$file = $vehiclesmodel->vehimage = $request->vehimage ;
// ---Somewhere here I have to declare vehimage as an array and loop through it ---
$filename = $file->getClientOriginalName() ;
$destinationPath = 'storage/veh-images' ;
$vehiclesmodel->vehimage = $filename;
$uploadSuccess = $file->move($destinationPath, $filename);
$vehiclesmodel->save() ;
return redirect('mypage')->with('success', 'The record has been added succesfully!') ;
}
VehiclesForm.blade.php
<form action="{{ route('mypage.store') }}" method="post" enctype="multipart/form-data">
@CSRF
<div class="form-group">
<label for="text">Model:</label>
<input type="text" class="form-control" name="vehmod" placeholder="Insert the model of the vehicle">
</div>
<div class="form-group">
<label for="text">Mark:</label>
<input type="text" class="form-control" name="vehmark" placeholder="Insert the mark of the vehicle">
</div>
<div class="form-group">
<label for="number">Year:</label>
<input type="number" class="form-control" name="vehyear" placeholder="Insert the year of the vehicle">
</div>
<div class="form-group">
<label for="text">Engine:</label>
<input type="text" class="form-control" name="vehengine" placeholder="Insert the engine of the vehicle">
</div>
<div class="form-group">
<label for="text">Transmission:</label>
<input type="text" class="form-control" name="vehtrans" placeholder="Insert the transmission of the vehicle">
</div>
<div class="form-group">
<label for="pic">Vehicles pictures:</label> <br>
<input type="file" name="vehimage[]" multiple>
</div>
<button type="submit" class="btn btn-primary">Add record</button>
</form>
There are various methods to do this. I've used below one, in which all images are store in a cell with implode(|). You have to change column type to text if you want to store all images in single column of a row
public function store(request $request) {
$destinationPath = 'storage/veh-images' ;
$images=array();
if($files=$request->file('vehimage')){
foreach($files as $file){
$filename=$file->getClientOriginalName();
$file->move($destinationPath, $filename);
$images[]=$name;
}
}
//implode images with pipe symbol
$allImages = implode("|",$images);
$vehiclesmodel = new VehiclesModel ;
$vehiclesmodel->vehmod = $request->vehmod ;
$vehiclesmodel->vehmark = $request->vehmark ;
$vehiclesmodel->vehyear = $request->vehyear ;
$vehiclesmodel->vehengine = $request->vehengine ;
$vehiclesmodel->vehtrans = $request->vehtrans ;
$vehiclesmodel->vehimage = $allImages;
$vehiclesmodel->save() ;
return redirect('mypage')->with('success', 'The record has been added succesfully!') ;
}
You can also try with json
Please or to participate in this conversation.