Seeker1337
3 months ago
902
13
Laravel

Laravel 6+ Upload multiple images through a form

Posted 3 months ago by Seeker1337

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>

Please sign in or create an account to participate in this conversation.