arneetsingh/laravel-customsort 问题修复 & 功能扩展

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

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

arneetsingh/laravel-customsort

Composer 安装命令:

composer require arneetsingh/laravel-customsort

包简介

A package to manually sort records of a eloquent model in custom order

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Problem it solves:

Say there is a need to show certain records of posts, eg. featured posts on top in your listing page. Just a custom order set by user to show specific posts in a specific order on top.

Installation

You can install the package via composer:

composer require arneetsingh/laravel-customsort

Publish and run the migrations with:

php artisan vendor:publish --tag="laravel-customsort-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="laravel-customsort-config"

Usage

Model

Use the CanCustomSort trait in your model you want to have the ability of sorting manually.

<?php

namespace App\Models;

use ArneetSingh\CustomSort\Traits\CanCustomSort;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use CanCustomSort;
}

Setting the order

Lets say we have 5 posts in our Post model. and we want to set them in order of ids - 2,4,1,5,3

Post::setNewOrder([2, 4, 1, 5, 3]);

Do note you don't need to pass all of your posts, you can though. Just the ones passed will be returned on top in that order.

Fetching in Custom Order

Post::orderByCustom()->get();

Will return post in order set first, and then rest of posts.

Set Priority per model basis

$post->setOrderPriority(10);

If the priority no is higher than other posts, it will be moved to top in results.

Example

Lets say we have 5 posts in our Post model. and we want to set them in order of ids - 2,4,1,5,3

Request: PUT
Endpoint: /posts/customSort
Payload:
{
	"custom_sort":[
		{
			"id":2,
			"priority":5			
		},
		{
			"id":4,
			"priority":4			
		},
		{
			"id":1,
			"priority":3			
		},
		{
			"id":5,
			"priority":2			
		},
		{
			"id":3,
			"priority":1			
		}
	]
}

Controller code could look like

    public function store(Request $request)
    {
        $request->validate([
            'custom_sort' => 'required',
            'custom_sort.*.id' => "required|exists:posts,id",
            'custom_sort.*.priority' => 'required|integer'
        ]);
        
        $morphClass = (new Post())->customSort()->getMorphClass();
        collect($request->custom_sort)->transform(function ($item) use($morphClass) {
            CustomSort::create([
                'sortable_id' => $item['id'],
                'sortable_type' => $morphClass,
                'priority' => $item['priority']
            ]);
        });
    }

Frontend Tips

I used SortabelJS for having the ability to drag and drop to set manual order. And here is snippet to javascript code.

let posts = []

// SortableJS onEnd handler would look like this
onEnd: function ({ oldIndex, newIndex }){
  const movedItem = posts.splice(oldIndex, 1)[0]
  posts.splice(newIndex, 0, movedItem)
}

// prepare payload
preparePayload: function() {
  const order = posts.map((item, key) => {
    return {
      id: item.id,
      priority: posts.length - key
    }
  })
  return { custom_sort: order }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

arneetsingh/laravel-customsort 适用场景与选型建议

arneetsingh/laravel-customsort 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「arneetsingh」 「laravel-customsort」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-19