involix/sushi
Composer 安装命令:
composer require involix/sushi
包简介
Eloquent's missing "array" driver.
README 文档
README
Eloquent's missing "array" driver.
Sometimes you want to use Eloquent, but without dealing with a database.
This Package Is Sponsorware 💰💰💰
Originally, this package was only available to my sponsors on GitHub Sponsors until I reached 75 sponsors.
Now that we've reached the goal, the package is fully open source.
Enjoy, and thanks for the support! ❤️
Install
composer require calebporzio/sushi
Use
Using this package consists of two steps:
- Add the
Sushitrait to a model. - Add a
$rowsproperty to the model.
That's it.
class State extends Model { use \Sushi\Sushi; protected $rows = [ [ 'abbr' => 'NY', 'name' => 'New York', ], [ 'abbr' => 'CA', 'name' => 'California', ], ]; }
Now, you can use this model anywhere you like, and it will behave as if you created a table with the rows you provided.
$stateName = State::whereAbbr('NY')->first()->name;
This is really useful for "Fixture" data, like states, countries, zip codes, user_roles, sites_settings, etc...
Relationships
Let's say you created a Role model, based on an array using Sushi, that looked like this:
class Role extends Model { use \Sushi\Sushi; protected $rows = [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; }
You can add a relationship to another standard model, just like you normally would:
class User extends Model { ... public function role() { return $this->belongsTo(Role::class); } }
Assuming the users table has a role_id column, you can do things like this:
// Grab a User. $user = User::first(); // Grab a Role. $role = Role::whereLabel('admin')->first(); // Associate them. $user->role()->associate($role); // Access like normal. $user->role; // Eager load. $user->load('role'); User::with('role')->first();
Note: There is one caveat when dealing with Sushi model relationships. The
whereHasmethod will NOT work. This is because the two models are spread across two separate databases.
How It Works
Under the hood, this package creates and caches a SQLite database JUST for this model. It creates a table and populates the rows. If, for whatever reason, it can't cache a .sqlite file, it will default to using an in-memory sqlite database.
Using ->getRows()
You can optionally opt out of using the protected $rows property, and directly implement your own getRows() method.
This will allow you to determine the rows for the model at runtime. You can even generate the model's rows from an external source like a third-party API.
Note: If you choose to use your own ->getRows() method, the rows will NOT be cached between requests.
class Role extends Model { use \Sushi\Sushi; public function getRows() { return [ ['id' => 1, 'label' => 'admin'], ['id' => 2, 'label' => 'manager'], ['id' => 3, 'label' => 'user'], ]; } }
involix/sushi 适用场景与选型建议
involix/sushi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 involix/sushi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 involix/sushi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-09