Hello Developers,
In this lesson, I will show to how to excel import in Laravel, also I will show you how to handle the Date
. Many times I have faced this issue, when you upload Date
in Laravel with excel import, it will not show, and sometimes Excel date formatting will not be recognized by Laravel. I will show you how to handle this with a special trick. At the initial level, we need to download a package. The process is given below:
First Step: Installing the maatwebsite/excel package
Require this package composer.json
for your Laravel project. This will download the package and PhpSpreadsheet.
composer require maatwebsite/excel
The Maatwebsite\Excel\ExcelServiceProvider
is auto-discovered and registered by default, but if you want to register it yourself:
config/app.php
'providers' => [
/*
* Package Service Providers...
*/
Maatwebsite\Excel\ExcelServiceProvider::class,
]
The Excel
facade is also auto-discovered, but if you want to add it manually:
config/app.php
'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
To publish the config, run the vendor publish command:
php artisan vendor:publish
Second Step: Create a route
In the web.php file, you have to create a route to upload the excel and store the function.
routes/web.php
Route::get('/sample_import', [SampleUpload::class, 'sample_import'])->name('excel.sample_import');
Route::post('/import_store', [SampleUpload::class, 'import_store'])->name('excel.import_store');
Third Step: Create the function
In the SampleUpload Controller, create a function.
app/Http/Controllers/SampleUpload.php
public function sample_import()
{
return view('sample_import');
}
In the views folder, a form needs to create to upload the excel sheet. In my case, the excel file has only one date column.
resources/views/sample_import.blade.php
<div class="row">
<div class="col-md-12 stretch-card">
<div class="card">
<div class="card-body">
<h6 class="card-title">Painter Import</h6>
<form action="{{ route('excel.painter_import_store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="file" class="form-control" name="file" id="file">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary submit">Painter Upload</button>
</form>
</div>
</div>
</div>
</div>
Fourth Step: Create the store function to upload the file
app/Http/Controllers/SampleController.php
public function painter_import_store(Request $request)
{
$excelDataArray = \Maatwebsite\Excel\Facades\Excel::toArray(new PainterImport(), $request->file);
foreach ($excelDataArray[0] as $key => $row) {
if ($key == 0) {
continue;
}
$excel_date = $row[6];
if ($excel_date = null) {
$DOB = null;
} else {
$unix_date = ($excel_date - 25569) * 86400;
$excel_date = 25569 + ($unix_date / 86400);
$unix_date = ($excel_date - 25569) * 86400;
$DOB = gmdate("Y-m-d", $unix_date);
}
$sample = new Sample();
$sample->DOBNew = $DOB;
$sample->save()
}
}
}
return view('index');
}
The above function will convert the excel Date
into the required date format.
Hope this might help you in your journey of development.
Read More: Remove Public Path From URL in Laravel Without htaccess