定制 dlucca/backpack-import 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dlucca/backpack-import

Composer 安装命令:

composer require dlucca/backpack-import

包简介

Import operation for Backpack Admin Panel

README 文档

README

dlucca/backpack-import is a Laravel package and a Backpack Laravel addon. Allows you to add a "import operation" to your CRUDs to easily import records from an excel file into to your database. This is a personal project and still under development, its not recommended to use it in a production environment. If you want to play with it, you can install this package as a local composer package, as still is not available via composer install.

Features

  • Import records into your DB from an excel file
  • Validations
  • Customize import logic
  • UI for file upload

Future features

  • Table with a preview of the data to be imported

Preview

Coming soon...

Example

...
class CityCrudController extends CrudController
{
    ...
    use \Dlucca\BackpackImport\ImportOperation;

    protected function setupImportOperation()
    {
        $this->backpackImport->setUpdateField('code');
        
        $this->backpackImport->setImportValidationRules([
            'name' => ['required'],
        ]);

        $this->backpackImport->setImportColumnMapping([
            'name' => 'Ciudad',
            'code' => 'Codigo',
            'created_at' => [
                'name' => 'Fecha',
                'import_logic' => function(&$entry, $row, $value) {
                    if ($value) {
                        $entry->created_at = Date::excelToDateTimeObject($value);
                    }
                },
            ],
        ]);
    }
    ...

Documentation

  • Setup
  • Insert records
  • Update records
  • Validations
  • Relationships
  • Views
  • Hooks
  • Advance options

Setup

Install with composer

    composer require dlucca/backpack-import:dev-master

To start, you have to use the operation in your backpack crud by adding the trait use \Dlucca\BackpackImport\ImportOperation;.Then you should create a function protected function setupImportOperation(). Inside this function you will have access to the Importer object via $this->backpackImport.

Insert records

To import records into to your database yo have to tell importer which columns from the excel correspond to your table fields, you can do it using the method $this->backpackImport->setImportColumnMapping(), this method accepts and associative array where the keys is the name of your field in the table and the value is the name in your excel column header. For example, lets assume we have a excel file in which the header columns are "Name" and "Age", and the fields in our people table are "first_name" and "person_age"

$this->backpackImport->setImportColumnMapping([
  'first_name' => 'Name',
  'person_age' => 'Age',
]);

Advanced options

For more complex operations, you can provide additional options to the array:

$this->backpackImport->setImportColumnMapping([
    'complex_field' => [
        'name' => 'Nombre'                  // Name of the column in the excel file
        'set_after_save' => true            // If true, the import logic will be execute after the model
                                            // was saved
        'import logic' => function (&$entry, $row, $value, $operation) {     // Provide a custom logic for the import

        },
        'fake' => true                      // If true, makes the column data available in the row
                                            // variable, but it wont execute any import logic or be assign
                                            // to any property on the model. It is usefully when you need to
                                            // use that data in other field
    ]
]);

Update Records

For default, every time you do a import, the package will try to insert the records in the database, even if already exists. If you want to update the records that already exist, you can set an "update field". The package will do a search by the field provided, and will update the record if there any result.

$this->backpackImport->setUpdateField('code');

Validations

When performing the upload and import of the excel file, you can set validations just that you would do in your typical create/update CRUD.

Set validations rule

$this->backpackImport->setImportValidationRules([
    'name' => ['required'],
]);

Set validation attributes names

$this->backpackImport->setImportValidationAttributes([
    'name' => 'Nombre',
]);

Hooks

Hooks are functions that you can attach to execute in different moments of the import operation. List of available hooks

$this->backpackImport->doHook('before_import', function ($mappedRows) {});
$this->backpackImport->doHook('before_insert', function ($entry, $row) {});
$this->backpackImport->doHook('after_insert', function ($entry, $row) {});
$this->backpackImport->doHook('after_import', function () {});

Set template example for download

Into setupImportOperation() add:

    protected function setupImportOperation()
    {
        ...

        $this->crud->set('import.example_file_url', Storage::url('file.xlsx'));

        ...

    }

dlucca/backpack-import 适用场景与选型建议

dlucca/backpack-import 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 302 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dlucca/backpack-import 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dlucca/backpack-import 我们能提供哪些服务?
定制开发 / 二次开发

基于 dlucca/backpack-import 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 302
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-02-24