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',
];
use EightyNine\ExcelImport\Tables\ExcelImportRelationshipAction;
class PostsRelationManager extends RelationManager
{
protected static string $relationship = 'posts';
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('title')
->columns([
Tables\Columns\TextColumn::make('title'),
])
->filters([
//
])
->headerActions([
ExcelImportRelationshipAction::make()
->slideOver()
->color('primary')
->validateUsing([
'title' => '
use EightyNine\ExcelImport\EnhancedDefaultImport;
use Illuminate\Support\Collection;
class CustomUserImport extends EnhancedDefaultImport
{
protected function beforeCollection(Collection $collection): void
{
// Validate ithError('Too many rows. Maximum 1000 allowed.');
}
// Access custom data from the form
$formData = $this->customImportData;
if (isset($formData['department_id'])) {
$departmentExists = Department::where('id', $formData['department_id'])->exists();
$this->validateCustomCondition(
$departmentExists,
'Selected department does not exist.'
);
}
}
protected function beforeCreateRecord(array $data, $row): void
{
// Row-level validation
if (User::where('email', $data['email'])->exists()) {
$this->stopImportWithWarning(
"User with email {$data['email']} already exists."
);
}
}
protected function afterCollection(Collection $collection): void
{
// Show success message with statistics
$count = $collection->count();
$this->stopImportWithSuccess("Successfully imported {$count} users!");
}
}
use EightyNine\ExcelImport\Exceptions\ImportStoppedException;
class MyExistingImport implements ToCollection, WithHeadingRow
{
public function collection(Collection $collection)
{
// Your existing validation logic
if ($someCondition) {
throw new ImportStoppedException('Custom error message', 'error');
}
// Continue with normal processing...
}
}
return [
// ...other config options
/**
* Load custom stylesheet
*
* Set to true to enable loading the custom CSS.
* Set to false to disable loading to prevent conflicts
* with existing button styles in your application
*/
'load_stylesheet' => true,
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.