erikgall/listify
Composer 安装命令:
composer require erikgall/listify
包简介
Turn any Eloquent model into a list! http://erikgall.github.io/listify
README 文档
README
Turn any Eloquent model into a list!
Description
This project was originally created by Travis Vignon but is no longer maintained and only supported Laravel 5. This fork of the project provides support for Laravel 7 & 8 applications and will be updated as new Laravel versions are released. Please feel free to send in contributions and help keep this package up to date!
Listify provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a position column defined as an integer on the mapped database table. Listify is an Eloquent port of the highly useful Ruby gem acts_as_list (https://github.com/swanandp/acts_as_list).
- Description
- Requirements
- Installation
- Quickstart
- Overview
- Configuration
- Notes
- Future Plans
- Contributing to Listify
- Copyright
Requirements
- Listify currently requires php >= 7.2.5
- Laravel 7 or Laravel 8
Installation
Install Listify into your Laravel 8 application with composer:
$ composer require erikgall/listify
The Laravel 7 branch can be found here erikgall/listify:2.x
To install Listify into your Laravel 7 application with composer:
$ composer require erikgall/listify:^2.0
Quickstart
First things first, you'll need to add a column to store the position. From the command line, use the migration generator:
php artisan listify:attach {table_name} {position_column_name}
php artisan migrate
{table_name}is a required argument.{position_column_name}is optional and the default value is "position".
Then, in your model:
<?php use EGALL\Listify\Listify; class User extends Eloquent { use Listify; ... }
Make sure that the
initListify()method is called afterparent::__construct()of your model.
That's all it takes to get access to the Listify hotness.
Overview
Instance Methods Added To Eloquent Models
You'll have a number of methods added to each instance of the Eloquent model to which Listify is added.
In Listify, "higher" means further up the list (a lower position), and "lower" means further down the list (a higher position). That can be confusing, so it might make sense to add tests that validate that you're using the right method given your context.
Methods That Change Position and Reorder List
eloquentModel.insertAt(int)eloquentModel.moveLower()will do nothing if the item is the lowest itemeloquentModel.moveHigher()will do nothing if the item is the highest itemeloquentModel.moveToBottom()eloquentModel.moveToTop()eloquentModel.removeFromList()
Methods That Change Position Without Reordering List Immediately
Note: a changed position will still trigger updates to other items in the list once the model is saved
eloquentModel.incrementPosition()eloquentModel.decrementPosition()eloquentModel.setListPosition(3)
Methods That Return Attributes of the Item's List Position
eloquentModel.isFirst()eloquentModel.isLast()eloquentModel.isInList()eloquentModel.isNotInList()eloquentModel.higherItem()eloquentModel.higherItems()will return all the items aboveeloquentModelin the list (ordered by the position, ascending)eloquentModel.lowerItem()eloquentModel.lowerItems()will return all the items beloweloquentModelin the list (ordered by the position, ascending)
Configuration
There are a few configuration options available. You'll need to pass these in as an array argument for initListify() in your model's constructor. Here are the options:
top_of_listsets the integer position for the top of the list (default:1).columnsets the name of your position column that you chose during installation (default:'position').add_new_atsets the name of your position column that you chose during installation (default:'bottom', options:'top'or'bottom').scopeallows you to scope the items in your list. This one requires a bit of elaboration. There are three posible values accepted:stringIlluminate\Database\Eloquent\Relations\BelongsToobjectIlluminate\Database\Query\Builderobject
String
If string is passed in, a raw string is passed in as a whereRaw to the scope. This allows you to do something like 'custom_foreign_key = 42' and have all of the items scoped to that result set. You can pass as complicated of a where clause as you want, and it will be passed straight into each DB operation.
Example:
<?php use EGALL\Listify\Listify; class User extends Eloquent { use Listify; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->listifyConfig->setScope('answer_to_ltuae = 42'); } }
Results in a scope of:
WHERE answer_to_ltuae = 42
Illuminate\Database\Eloquent\Relations\BelongsTo
If Illuminate\Database\Eloquent\Relations\BelongsTo is passed in, Listify will match up the foreign key of the scope to the value of the corresponding foreign key of the model instance.
Example:
<?php use EGALL\Listify\Listify; class ToDoListItem extends Eloquent { use Listify; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->listifyConfig->setScope($this->toDoList()); } public function toDoList() { $this->belongsTo('ToDoList'); } }
Results in a scope of:
WHERE to_do_list_id = {{value of toDoListItem.to_do_list_id}}
Illuminate\Database\Query\Builder
And lastly, if Illuminate\Database\Query\Builder is passed in, Listify will extract the where clause of the builder and use it as the scope of the Listify items. This scope type was added in an attempt to keep parity between the acts_as_list version and Listify; however, due to differences in the languages and in ActiveRecord versus Eloquent, it is a limited implementation so far and needs impovement to be more flexible and secure. This is a big limitation and will be the first thing addressed in upcoming releases.
This one is tricky, because in order for it to work the query objects where array is prepared with the bindings outside of PDO and then passed in as a raw string. So, please keep in mind that this route can open your application up to abuse if you are not careful about how the object is built. If you use direct user input, please sanitize the data before using this as a scope for Listify.
Example:
<?php class ToDoListItem extends Eloquent { use \EGALL\Listify\Listify; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->listifyConfig->setScope(DB::table($this->getTable())->where('type', '=', 'Not A List of My Favorite Porn Videos')); } }
Results in a scope of:
to_do_list_items.type = 'Not A List of My Favorite Porn Videos'
Changing the configuration
You may also change any configuration value during runtime by using any of the setters on the Config class. $this->listifyConfig->('key', 'value');`. For example, to change the scope, you can do this:
$this->listifyConfig->setScope('what_does_the_fox_say = "ring ding ding ding"');
When an update is processed, the original scope will be used to remove the record from that list, and insert it into the new list scope. Be careful here. Changing the configuration during processing can have unpredictable effects.
You may also change the other config values. The setters are chainable.
$this->listifyConfig->setTopPositionInList(0) ->setPositionColumnName('order') ->setScope($this->menu()) ->setAddNewItemTo(Config::POSITION_BOTTOM);
Notes
All position queries (select, update, etc.) inside trait methods are executed without the default scope, this will prevent nasty issues when the default scope is different from Listify scope.
The position column is set after validations are called, so you should not put a presence validation on the position column.
Future Plans
- Add support for using a closure as a scope
- Update
Illuminate\Database\Query\Builderscope to be more secure and flexible - Additional features for the install command. Things like:
- update the model with trait automatically (including init method in constructor)
- generate (or add to) a controller with actions for each public method for Listify, including adding necessary routes. This would make it easy to, say, call something like
http://localhost:8000/foos/1/move_lowerthrough an AJAX-y front end.
Aside from that, I hope to just keep in parity with the Ruby gem acts_as_list (https://github.com/swanandp/acts_as_list) as necessary.
Contributing to Listify
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
- Fork the project
- Start a feature/bugfix branch
- Commit and push until you are happy with your contribution
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Composer.json, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
- I would recommend using Laravel 8.0 and higher for testing the build before a pull request.
Copyright
Copyright (c) 2020 Erik Galloway, released under the MIT license
erikgall/listify 适用场景与选型建议
erikgall/listify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.64k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 12 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rails」 「order」 「model」 「list」 「laravel」 「position」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 erikgall/listify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 erikgall/listify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 erikgall/listify 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
AuthorityController is an PHP authorization library for Laravel 5 which restricts what resources a given user is allowed to access.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PHP client library for easily accessing Ruby on Rails-based REST services.
Interfaces for web resource models and services to retrieve and create them
Sylius plugin that adds the possibility to add products to the shopping cart using a button on the product card.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 15.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-19