定制 pion/laravel-eloquent-position 二次开发

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

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

pion/laravel-eloquent-position

Composer 安装命令:

composer require pion/laravel-eloquent-position

包简介

Position logic for Eloquent models with minimum setup

README 文档

README

Total Downloads Latest Stable Version Latest Unstable Version

Position logic for Eloquent models with minimum setup. Before saving it will check if the position has changed and updates the other entries based on the models position value.

Installation

Tested in Laravel 5.4 - 8.

Install via composer

composer require pion/laravel-eloquent-position

Usage

  1. Add a position (can be custom) column in your table (model)
  2. Add PositionTrait into your model (if you are using custom column set the $positionColumn property)
  3. If you are using grouped entries (like parent_id and etc), you can set the $positionGroup with the column name/names (supports single string or multiple columns)
  4. Add to form the position input (can be input[type=number] and etc) and fill/set the position on save
  5. When position is null or empty string, the last position will be used.
  6. If you are not using migration (the column exists), run the php artisian model:position` command to fix current entries (it will create correct order)

Then you can get your entries sorted:

// ASC
YourModel::sorted()->get()

// DESC
YourModel::sortedByDESC()->get()

If using default column name (position), the value will be converted to numeric value (if not null or empty string).

Get the position Use the $model->getPosition() or use the standard way by using the column name $model->position

Migration example

public function up()
    {
    Schema::table('pages', function (Blueprint $table) {
        $table->smallInteger('position')->default(0)->after('id');
    });

    // Update the order pages
    Artisan::call('model:position', [
        'model'=> \App\Models\Page\Page::class
    ]);
}

Model example

class Page extends Model
{
    use PositionTrait;

    public $table = 'pages';
    public $positionGroup = ['parent_slug'];

    protected $fillable = [
        'title', 'slug', 'parent_slug', 'content', 'description', 'position'
    ];
    
}

Events

You can listen to events for positioning changes. You can use the PositionEventsTrait for easy model registration.

....

class YourModel extends Model {
    use PositionTrait, PositionEventsTrait;
    ....
}

Positioning

Called before running the last position calculation and the final movement of other entries for given position.

Enables to:

  • Restore the position to original value - return false
  • Add additional query conditions via AbstractPositionQuery object in second parameter ($query->query() => Builder)

Name: positioning

YourModel::positioning(function($model, $query) {
    $query->query()->where('type', 'type'); // or etc
    \Log::info('positioning', 'To '.$model->getPosition().' from '.$query->oldPosition());
});

Positioned

Name: positioned

Example via trait:

YourModel::positioned(function($model) {
    /// TODO
});

Command

Reposition command

This command will help you to fix the order of your models. You must provide a model class. You must include the RecalculatePositionCommand into your Console Kernel class.

php artisan model:position App\\Models\\YourModel

Traits

PositionTrait

Uses the BasePositionTrait and PositionScopeTrait

You can set:

  • string positionColumn to enable overriding for the position column
  • boolean disablePositionUpdate disables the updated of other entries
  • string|array positionGroup builds a filter from columns for position calculation. Supports single column or multiple columns
  • string defaultPositionValue allows returning different value when position is empty string or null. Default value is null

PositionScopeTrait

Todo

  • Add the custom position trait to enable automatic convert to numeric value (don't want to use the setAttribute method) - In progress
  • Add service provider for automatic command registration
  • Add all docs for all features
  • Add next/prev scope functions in PositionScopeTrait
  • Add PositionHelperTrait with (getLastUsedPosition, getNextPosition($position = null))

Contribution

See CONTRIBUTING.md for how to contribute changes. All contributions are welcome.

Copyright and License

laravel-eloquent-position was written by Martin Kluska and is released under the MIT License.

Copyright (c) 2016 Martin Kluska

pion/laravel-eloquent-position 适用场景与选型建议

pion/laravel-eloquent-position 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.63k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2017 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 pion/laravel-eloquent-position 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 34.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-25