1. Go to this page and download the library: Download dev-pirate/lara-excel-craft library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dev-pirate / lara-excel-craft example snippets
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());
}
}
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
];