inmanturbo / futomaki
肥寿司卷
v2.0.14
2024-06-21 20:48 UTC
Requires
- php: ^8.2
- calebporzio/sushi: ^2.5.2
- envor/laravel-datastore: ^1.2.10
- envor/laravel-schema-macros: ^1.1.4
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.16.2
- spatie/simple-excel: ^3.4
Requires (Dev)
- larastan/larastan: *
- laravel/octane: ^2.3
- laravel/pint: *
- livewire/volt: ^1.6
- nunomaduro/collision: *
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.33
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.2
- spatie/docker: ^1.12
README
一套在calebporzio/sushi之上构建的功能集。
安装
您可以通过composer安装此包
composer require inmanturbo/futomaki
用法
Eloquent CSV 驱动
<?php use Illuminate\Database\Eloquent\Model; use Inmanturbo\Futomaki\HasCsv; class Post extends Model { use HasCsv; protected $schema = [ 'id' => 'id', 'title' => 'string', 'content' => 'text', ]; protected function csvFileName() { return 'posts.csv'; } protected function csvDirectory() { return storage_path('csv'); } }
HasCsv在底层使用sushi(数组驱动),并支持定义一个(rows)属性。将自动使用定义的(rows)创建csv文件。
use Illuminate\Database\Eloquent\Model; use Inmanturbo\Futomaki\HasCsv; class PostWithCsv extends Model { use HasCsv; protected $guarded = []; protected $schema = [ 'id' => 'id', 'title' => 'string', 'content' => 'text', ]; protected $rows = [ [ 'id' => 1, 'title' => 'Post 1', 'content' => 'Content 1', ], [ 'id' => 2, 'title' => 'Post 2', 'content' => 'Content 2', ], ]; }
使用 ->getCsvRows()
支持实现自己的getCsvRows()
方法。
use Illuminate\Database\Eloquent\Model; use Inmanturbo\Futomaki\HasCsv; class PostWithCsv extends Model { use HasCsv; protected $guarded = []; protected $schema = [ 'id' => 'id', 'title' => 'string', 'content' => 'text', ]; public function getCsvRows() { return [ ['id' => 1,'title' => 'Post 1', 'content' => 'Content 1'], ['id' => 2,'title' => 'Post 2','content' => 'Content 2'], ]; } }
HasFutumakiWrites
HasFutomakiWrites是一个特性,它利用eloquent的saving()
和deleting()
钩子来支持将sushi的更改写入另一个数据库、API等。以下是一个示例。
use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Inmanturbo\Futomaki\Futomaki; use Inmanturbo\Futomaki\HasFutomakiWrites; class PostWithFutomakiWrites extends Model { use Futomaki; use HasFutomakiWrites; public $timestamps = true; public $guarded = []; protected $schema = [ 'id' => 'id', 'title' => 'string', 'content' => 'text', ]; public function getRows() { return DB::connection('remote_posts')->table('posts')->get()->map(fn ($remoteItem) => [ 'id' => $remoteItem->id, 'title' => $remoteItem->title, 'content' => $remoteItem->body, ])->toArray(); } public function futomakiSaving() { $values = [ 'id' => $this->id, 'title' => $this->title, 'body' => $this->content, ]; DB::connection('remote_posts')->transaction(function () { DB::connection('remote_posts')->table('posts')->upsert($values, $this->getKeyName()); }); } public function futomakiDeleting() { DB::connection('remote_posts') ->table('posts') ->where($this->getKeyName(), $this->getKey()) ->delete(); } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。