LaraExcelCraft is a Laravel package for importing/exporting Excel file into database tables easily step by step with nice UI, using antd components, ReactJs and phpoffice/phpspreadsheet.
Run the following command to pull in the latest version:
composer require dev-pirate/lara-excel-craft
Run the following command to publish the package config file:
php artisan vendor:publish --provider="DevPirate\LaraExcelCraft\Providers\LaraExcelCraftProvider"
You should now have a config/lara-excel-craft.php file that allows you to configure the basics of this package.
Add this code inside your route file:
Route::middleware([
'api',
\Fruitcake\Cors\HandleCors::class,
])->group(function() {
LaraExcelCraft::routes();
});
// \Fruitcake\Cors\HandleCors middleware are required here to manage cors
- Add the import component to your view :
<x-lara-excel-craft::lara-excel-import-sheet />
- Add the export component to your view :
<x-lara-excel-craft::lara-excel-export-sheet />
Before continuing, make sure you have installed the package as per the installation instructions for Laravel.
Firstly you need to implement the DevPirate\LaraExcelCraft\Interfaces\ExcelManager interface on your model, which require a custom data importing logic, you implement the 3 methods importDataFromExcel(array $data) and getImportableFields() and exportDataToExcel().
The example below should give you an idea of how this could look. Obviously you should make any changes, as necessary, to suit your own needs.
<?php
namespace App\Models;
use Carbon\Carbon;
use DevPirate\LaraExcelCraft\Interfaces\ExcelManager;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Example extends Model implements ExcelManager
{
use HasFactory;
protected $fillable = [
'orderDate',
'region',
'rep',
'item',
'unit',
'total',
'created_at',
'updated_at',
];
public static function importDataFromExcel(array $data): void
{
// this can be customized, it depends on your logic
// this is just an example
$data = array_map(function ($item) {
return [
...$item,
'total' => floatval($item['total'] ?? 0),
'unit' => intval($item['unit'] ?? 0),
'orderDate' => $item['orderDate'] ? Carbon::createFromFormat('d/m/Y', trim($item['orderDate'])): null,
'created_at' => now(),
'updated_at' => now(),
];
}, $data);
self::insert($data);
}
public static function getImportableFields(): array
{
// return an array of the table fields that could be importable from excel
return [
'orderDate',
'region',
'rep',
'item',
'unit',
'total'
];
}
public static function exportDataFromExcel(): array
{
// this can be customized depend on your logic
return array_map(function ($item) {
return array_merge($item, [
'orderDate' => Carbon::parse($item['orderDate'])->format('d/m/Y') ?? ''
]);
}, self::all()->toArray());
}
}
Let's review some of the options in the config/lara-excel-craft.php file that we published earlier.
First up is:
<?php
return [
// storage disk name where the uploaded temp excel files are going to be stored
'fileTempDisk' => 'local',
// path where your application models classes are stored
'models_path' => app_path('Models'),
// route name where you want the application to redirect you after importing the data with excel sheet
'redirectTo' => 'home'
// other configuration parameters
];
.