Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to download excel template from storage before upload

I am developing a web application that imports from excel sheet and save into the database using maatwebsite-excel 3.1 and Laravel-5.8

public function collection(Collection $rows)
{           

    foreach($rows as $row)
    { 
        if(!$this->alreadyExist($row)){

            $enrollment = Enrollment::create([
                
                'surname' => $row['surname'],
                'other_names' => $row['other_names'],
                'dob' => $this->toPHPdate($row['dob']),
                'gender' => $row['gender'],
                'home_address' => $row['home_address'],
                'nationality' => $row['nationality'],
                'state' => $row['state_of_origin'],
                'lga' => $row['lga'],
                'town' => $row['town'],
                'village' => $row['village'],
                'admitted_into' => $this->isFilled($row['admitted_into']) && $row['admitted_into'] > 0 ? $row['admitted_into'] : null,
                'src' => 'import',
                ]);
                               


            if(isset($row['father_fullname']) && $this->isFilled($row['father_fullname']))
            {
                $father = Parentt::create([
                    'enrollment_id' => $enrollment->id,
                    'fullname' => $row['father_fullname'],
                    'relation' => 'father',
                    'phone' => $row['father_phone'],
                ]);
               

            }
            if(isset($row['mother_fullname']) && $this->isFilled($row['mother_fullname']))
            {
                $mother = Parentt::create([
                    'enrollment_id' => $enrollment->id,
                    'fullname' => $row['mother_fullname'],
                    'relation' => 'mother',
                    'phone' => $row['mother_phone'],
                ]);
                 
            }
            
            if($this->isFilled($row['admitted_into']) && $row['admitted_into'] > 0 ){
               $student = Student::create([
                    'enrollment_id' => $enrollment->id,
                    'classroom_id' => $row['admitted_into']
                ]);
               
            }

        }else{
            $enrollment = Enrollment::where('surname',$row['surname'])->where('other_names',$row['other_names'])->first();
            $enrollment->dob = $this->toPHPdate($row['dob']);
            $enrollment->gender = $row['gender'];
            $enrollment->home_address = $row['home_address'];
            $enrollment->nationality = $row['nationality'];
            $enrollment->state = $row['state_of_origin'];
            $enrollment->lga = $row['lga'];
            $enrollment->town = $row['town'];
            $enrollment->village = $row['village'];
            $enrollment->admitted_into = $this->isFilled($row['admitted_into']) && $row['admitted_into'] > 0 ? $row['admitted_into'] : null;
            $enrollment->src = 'import';
            $enrollment->save();

            if($enrollment->parents->count() > 0){
                foreach($enrollment->parents as $parent){
                    if($parent->relation == 'mother'){
                        $parent->fullname = $row['mother_fullname'];
                        $parent->phone = $row['mother_phone'];
                        $parent->save();
                    }
                    else if($parent->relation == 'father'){
                        $parent->fullname = $row['father_fullname'];
                        $parent->phone = $row['father_phone'];
                        $parent->save();
                    }
                }
            }else{
                $father = Parentt::create([
                    'enrollment_id' => $enrollment->id,
                    'fullname' => $row['father_fullname'],
                    'relation' => 'father',
                    'phone' => $row['father_phone'],
                ]);

                $mother = Parentt::create([
                    'enrollment_id' => $enrollment->id,
                    'fullname' => $row['mother_fullname'],
                    'relation' => 'mother',
                    'phone' => $row['mother_phone'],
                ]);
            }
        }
    }
}

Import Controller

public function studentForm(){
    return view('import.students');
}
public function importStudents(){
    Excel::import(new StudentsImport,request()->file('file'));
    return back()->with('success','Students records imported');
}

index.blade

    <div class="float-right">
            <button type="button" class="btn btn-info float-right" data-toggle="modal" data-target="#importExcel" title="Type of file .xlsx,.xls">
              <i class="fas fa-upload"></i> Import Students
            </button>
        
    </div>

            <div class="modal fade" id="importExcel" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
                aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <form method="post" action="" enctype="multipart/form-data">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Import Employees</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <!--<br>-->
    <div class="row">
        <div class="col-sm-12 col-xs-12">
                                      <div class="card-body">
            <div class="callout callout-info">
                <h5> You must have to follow the following instruction at the time of importing data</h5>
                <ol>
                    <li><b> The field with red color are the required field cannot be blank.</b></li>
                    <li> Birth date must be less than current date.</li>
                    <li> All dates must be in this format: dd/mm/yyyy.</li>
                </ol>
                <h5> Download the sample format of Excel sheet.<b></b></h5>
                                     </div>
            </div>   
        </div><!--./col-->
    </div><!--./row-->
                            <div class="modal-body">
                                @csrf
                                <div class="custom-file">
                                <input type="file" name="file" class="custom-file-input" id="customFile" required="required">
                                <label class="custom-file-label" for="exampleInputFile">Choose Excel File: .xlsx, .xls</label>
                                </div>

                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="submit" class="btn btn-primary">Import</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

before users will import, I want them to be able to download the excel format for the import and go through it. The link will be available on the index.blade form for the import/upload.

The is the template path:

storage/companies/studentfile

How do I achieve this?

Thank you.

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

@noblemfd

you can add a link to the file:

<a href="{{ Storage::url('companies/studentfile.xlsx') }}" download> Download Excel Template </a>

Please or to participate in this conversation.