flyhjaelp/laravel-eloquent-orderable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

flyhjaelp/laravel-eloquent-orderable

Composer 安装命令:

composer require flyhjaelp/laravel-eloquent-orderable

包简介

Make your Eloquent models orderable by using the orderable trait on them. Which automatically updates the order of all other models within it's group when the order of a current model is updated or a new model insereted or deleted.

README 文档

README

Laravel Eloquent Orderable is a package that helps you make your eloquent models orderable, either within a group our within all other models of the same class.

Installation

Install via composer

composer require flyhjaelp/laravel-eloquent-orderable

Database setup

If you want to use the orderable functionality on a model it has to have a database column it can be ordered by. By default the package will look for a column named "order", but this can be overwritten. The order column should be an unsigned integer that's nullable. Example:

Schema::create('orderable_test_models', function (Blueprint $table) {
  $table->unsignedInteger('order')->nullable();
});

Default Usage

<?php

use Flyhjaelp\LaravelEloquentOrderable\Interfaces\OrderableInterface;
use Flyhjaelp\LaravelEloquentOrderable\Traits\Orderable;
use Illuminate\Database\Eloquent\Model;

class Foobar extends Model implements OrderableInterface { //implement the orderable interface

   use Orderable; //use the orderable trait
   
}

Creating a new model without a specified order

New instances will now have an order added to them, by default they are added as last in order.

$foobarA = (new Foobar())->save();
$foobarB = (new Foobar())->save();
$foobarC = (new Foobar())->save();
Foobar::all()->pluck('order','id');
// will output [1 => 1, 2 => 2, 3 => 3]

Creating a new model with a specified order

If an order is specified when being created that order will update already existing orders accordingly.

$foobarD = new Foobar();
$foobarD->order = 2;
$foobarD->save();
Foobar::all()->pluck('order','id');
// will output [1 => 1, 4 => 2, 2 => 3, 3 => 4]

Updating the order of a model

When updating a models order the other models automatically update their orders accordingly.

$foobarC->order = 2;`
$foobarC->save();
Foobar::all()->pluck('order','id'); // will output [1 => 1, 3 => 2, 4 => 3, 2 => 3]

Deleting a model

When deleting a model, the order of all other models with a higher order will have their order decreased by one.

$foobarA->delete();
Foobar::all()->pluck('order','id'); // will output [3 => 1, 4 => 2, 2 => 3]

Grouping usage

You can make a group within your model, and the order only applies within the group. Example you might have a model called MenuItem which should be grouped by menu_id, and the order should only apply within it's group. To add a group to model you have to include the orderableWithinGroup trait and implement the following functions:

  • scopeOrdered(Builder $query)
  • scopeWithinOrderGroup(Builder $query, OrderableInterface $orderableModel)
  • columnsAffectingOrderGroup()
<?php

use Flyhjaelp\LaravelEloquentOrderable\Interfaces\OrderableInterface;
use Flyhjaelp\LaravelEloquentOrderable\Traits\OrderableWithinGroup;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

class MenuItem extends Model implements OrderableInterface { //implement the orderable interface

   use OrderableWithinGroup; //use the orderableWithinGroup trait
   
   public function scopeOrdered(Builder $query): void{
      $query->orderBy('menu_id')->orderBy('order');
   }
   
   public function scopeWithinOrderGroup(Builder $query, OrderableInterface $orderableModel): void{
      $query->where('menu_id',$orderableModel->menu_id);
   }
   
   public function columnsAffectingOrderGroup(): Collection{
      return collect(['menu_id']);
   }
   
}

Models are now ordered within their group

New instances will have an order added to them, by default they are added as last in order within their group

<?php

$foobarA = new Foobar();
$foobarA->menu_id = 1;
$foobarA->save();
$foobarB= new Foobar();
$foobarB->menu_id = 1;
$foobarB->save();
$foobarC = new Foobar();
$foobarC->menu_id = 2;
$foobarC->save();
Foobar::all()->pluck('order','id');
// will output [1 => 1, 2 => 2, 3 => 1]

Usage within pivot models

You can make pivot models orderable if you wish to be able order a many to many relationship whenever it's retrieved. The order only works one way, meaning if you for example have a journey that has mutiple checkpoints you can make the checkpoints come in the correct order when you retrieve them from the journey, but not the other way around.

Setup pivot model ordering

For a pivot model to be orderable you have to use the PivotOrderable trait on the model. It's also required to have an autoincrementing primary key(usually an "id") in the pivot relationship table. Besides that you have to implement the methods mentioned under OrderableWithinGroup

<?php

use Flyhjaelp\LaravelEloquentOrderable\Interfaces\OrderableInterface;
use Flyhjaelp\LaravelEloquentOrderable\Traits\PivotOrderable;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Builder;

class JourneyCheckpointsRelationship extends Pivot implements OrderableInterface{

   use PivotOrderable;

   public $incrementing = true;
   
   public function scopeWithinOrderGroup($query, OrderableInterface $orderableModel)
   {
      return $query->where('journey_id', $orderableModel->journey_id);
   }

   public function scopeOrdered(Builder $query): void
   {
      $query->orderBy('journey_id')->orderBy($this->getOrderableColumn());
   }

   public function columnsAffectingOrderGroup(): Collection
   {
      return collect(['journey_id']);
   }
   
}

When calling the relationship from a model, you have to chain on the using method on the belongsToMany call when defining the relationship on the model. Also you have to add orderBy method call if you want the relationship ordered when retrieved.

<?php

use Illuminate\Database\Eloquent\Model;

class Journey extends Model{

   public function checkpoints() {
      return $this
         ->belongsToMany(Checkpoint::class)
         ->using(JourneyCheckpointsRelationship::class)
         ->withPivot('order')
         ->orderBy('pivot_order');
   }

}

Overwriting default values

You have to change the default column used for storing the order in, as well as the default ordering scope.

Overwriting default ordering column

public function getOrderableColumn(): string {
  return 'non_default_order_column';
}

Overwriting global ordering scope

public function scopeOrdered(Builder $query): void{
  $query->orderBy('menu_id')->orderBy('order');
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

flyhjaelp/laravel-eloquent-orderable 适用场景与选型建议

flyhjaelp/laravel-eloquent-orderable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.53k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2019 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-12