Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
eighty9nine committed Oct 20, 2023
1 parent e16b360 commit 8b6acfc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
86 changes: 61 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# This is my package filament-excel-import
# Filament Excel Import

[![Latest Version on Packagist](https://img.shields.io/packagist/v/eightynine/filament-excel-import.svg?style=flat-square)](https://packagist.org/packages/eightynine/filament-excel-import)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/eightynine/filament-excel-import/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/eightynine/filament-excel-import/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/eightynine/filament-excel-import/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/eightynine/filament-excel-import/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/eightynine/filament-excel-import.svg?style=flat-square)](https://packagist.org/packages/eightynine/filament-excel-import)

This package adds a new feature to your filament resource, allowing you to easily import data to your model


This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
_This package brings the maatwebsite/laravel-excel functionalities to filament. You can use all the maatwebsite/laravel-excel features in your laravel project_

## Installation

Expand All @@ -17,37 +15,75 @@ You can install the package via composer:
composer require eightynine/filament-excel-import
```

You can publish and run the migrations with:
## Usage

```bash
php artisan vendor:publish --tag="filament-excel-import-migrations"
php artisan migrate
```
Before using this action, make sure to allow [Mass Assignment](https://laravel.com/docs/10.x/eloquent#mass-assignment) for your model. If you are doing a custom import, this is not necessary.

You can publish the config file with:
```php
<?php

```bash
php artisan vendor:publish --tag="filament-excel-import-config"
```
namespace App\Models;

Optionally, you can publish the views using
use Illuminate\Database\Eloquent\Model;

```bash
php artisan vendor:publish --tag="filament-excel-import-views"
class Client extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'phone', 'email'];
}
```

This is the contents of the published config file:
For example, if you have a 'ClientResource' in your project, integrate the action into ListClients class as demonstrated below:

```php
return [
];

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(),
];
}
}

```

## Usage
### Custom Import

If you wish to use your own import class to change the import procedure, you can use your own Import class.

```bash
php artisan make:import MyClientImport
```

Then in your action use your client imeport class
```php
$excelImportAction = new EightyNine\ExcelImportAction();
echo $excelImportAction->echoPhrase('Hello, EightyNine!');

protected function getHeaderActions(): array
{
return [
\EightyNine\ExcelImport\ExcelImportAction::make()
->slideOver()
->color("primary")
->use(App\Imports\MyClientImport::class),
Actions\CreateAction::make(),
];
}
```

## Testing
Expand All @@ -70,8 +106,8 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [Eighty Nine](https://github.com/eighty9nine)
- [All Contributors](../../contributors)
- [Eighty Nine](https://github.com/eighty9nine)
- [All Contributors](../../contributors)

## License

Expand Down
15 changes: 1 addition & 14 deletions src/ExcelImportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ class ExcelImportAction extends Action

protected ?string $disk = null;

public function class(string $class = null, ...$attributes): static
public function use(string $class = null, ...$attributes): static
{
$this->importClass = $class ?: DefaultImport::class;
$this->importClassAttributes = $attributes;

return $this;
}

public function disk(string $disk = null)
{
$this->disk = $disk;
}

protected function getDisk()
{
return $this->disk ?: config('filesystems.default');
Expand All @@ -49,20 +44,12 @@ public function action(Closure | string | null $action): static
return $this;
}

public function form(array | Closure | null $form): static
{
$this->form = $this->getDefaultForm();

return $this;
}

protected function getDefaultForm(): array
{
return [
FileUpload::make('upload')
->label(fn ($livewire) => str($livewire->getTable()->getPluralModelLabel())->title() . ' ' . __('Excel Data'))
->default(1)
->disk($this->getDisk())
->columns()
->required(),
];
Expand Down

0 comments on commit 8b6acfc

Please sign in to comment.