PHP code example of eightynine / filament-excel-import
1. Go to this page and download the library: Download eightynine/filament-excel-import 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/ */
eightynine / filament-excel-import example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'phone', 'email'];
}
namespace App\Filament\Resources\ClientResource\Pages;
use App\Filament\Resources\ClientResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListClients extends ListRecords
{
protected static string $resource = ClientResource::class;
protected function getHeaderActions(): array
{
return [
\EightyNine\ExcelImport\ExcelImportAction::make()
->color("primary"),
Actions\CreateAction::make(),
];
}
}
protected function getHeaderActions(): array
{
return [
\EightyNine\ExcelImport\ExcelImportAction::make()
->processCollectionUsing(function (string $modelClass, Collection $collection) {
// Do some stuff with the collection
return $collection;
}),
Actions\CreateAction::make(),
];
}
protected function getHeaderActions(): array
{
return [
\EightyNine\ExcelImport\ExcelImportAction::make()
->slideOver()
->color("primary")
->use(App\Imports\MyClientImport::class)
// Add fields before the upload field
->beforeUploadField([
TextInput::make('default_password'),
TextInput::make('default_status'),
])
// Or add fields after the upload field
->afterUploadField([
TextInput::make('default_password'),
TextInput::make('default_status'),
])
// Or customise the upload field
->uploadField(
fn ($upload) => $upload
->label("Some other label")
)
// Use the additional form fields data
->beforeImport(function (array $data, $livewire, $excelImportAction) {
$defaultStatus = $data['default_status'];
$defaultPassword = $data['default_password'];
// When adding the additional data, the data will be merged with
// the row data when inserting into the database
$excelImportAction->additionalData([
'password' => $defaultPassword,
'status' => $defaultStatus
]);
// When adding the custom import data, the data will be available in
// the custom import as $this->customImport data, when the custom import extends the
// Default import.
$excelImportAction->customImportData([
'other_details' => [ 1, 2, 3, 4],
'age' => 5
]);
// Do some other stuff with the data before importing
})
,
Actions\CreateAction::make(),
];
}
return [
/**
* File upload path
*
* Customise the path where the file will be uploaded to,
* if left empty, config('filesystems.default') will be used
*/
'upload_disk' => 's3',
];