tecdynamics / data-synchronize
此软件包最新版本(dev-main)没有可用的许可证信息。
几步即可使站点准备就绪
dev-main
2024-07-09 08:28 UTC
Requires
- maatwebsite/excel: ^3.1
- spatie/simple-excel: ^3.6
- tecdynamics/platform: *@dev
This package is auto-updated.
Last update: 2024-09-09 09:07:31 UTC
README
用法
导出器
创建导出器有两种方式。
使用命令创建导出器
您可以使用 php artisan data-synchronize:make:exporter
命令来创建导出器。
php artisan data-synchronize:make:exporter PostExporter
手动创建导出器
导出器应该看起来像这样,下面是一个 PostExporter
类的示例。
<?php namespace Tec\Blog\Exporters; use Tec\Blog\Models\Post; use Tec\DataSynchronize\Exporter\ExportColumn; use Tec\DataSynchronize\Exporter\Exporter; use Illuminate\Support\Collection; class PostExporter extends Exporter { public function label(): string { return 'Posts'; } public function columns(): array { return [ ExportColumn::make('name'), ExportColumn::make('description'), ExportColumn::make('created_at'), ]; } public function collection(): Collection { return Post::all(); } }
这样在控制器中使用导出器。
<?php namespace Tec\Blog\Http\Controllers; use Tec\DataSynchronize\Exporter\Exporter; use Tec\DataSynchronize\Http\Controllers\ExportController; use Tec\Blog\Exporters\PostExporter; class ExportPostController extends ExportController { protected function getExporter(): Exporter { return PostExporter::make(); } }
然后在你的路由文件中注册路由。
use Tec\Base\Facades\AdminHelper; use Illuminate\Support\Facades\Route; use Tec\Blog\Http\Controllers\ExportPostController; AdminHelper::registerRoutes(function () { Route::prefix('tools/data-synchronize')->name('tools.data-synchronize.')->group(function () { Route::group(['prefix' => 'export/posts', 'as' => 'export.posts.', 'permission' => 'posts.export'], function () { Route::get('/', [ExportPostController::class, 'index'])->name('index'); Route::post('/', [ExportPostController::class, 'store'])->name('store'); }); }); });
每个导出器路由都应该有访问权限。您可以使用路由组中的 permission
键来定义权限。
在上面的路由定义中,权限是 posts.export
键,其父权限是 tools.data-synchronize
。您可以在您插件的 permissions.php
文件中定义权限。
return [ [ 'name' => 'Export Posts', 'flag' => 'posts.export', 'parent_flag' => 'tools.data-synchronize', ], ];
现在您可以通过 http://your-domain/tools/data-synchronize/export/posts
导出帖子。
将导出器添加到“导出/导入数据”面板部分
要将导出器添加到“导出/导入数据”面板部分,您可以使用 PanelSectionManager
类的 beforeRendering
方法将导出器注册到面板部分。
use Tec\Base\Facades\PanelSectionManager; use Tec\Base\PanelSections\PanelSectionItem; use Tec\DataSynchronize\PanelSections\ExportPanelSection; public function boot(): void { // ... PanelSectionManager::setGroupId('data-synchronize')->beforeRendering(function () { PanelSectionManager::default() ->registerItem( ExportPanelSection::class, fn () => PanelSectionItem::make('posts') ->setTitle('Posts') ->withDescription('Export post data to CSV or Excel file.') ->withPriority(120) ->withRoute('tools.data-synchronize.export.posts.index') ->withPermission('posts.export') ); }); // ... }
您可以在 导出/导入数据 面板部分看到导出器。