定制 akaunting/laravel-sortable 二次开发

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

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

akaunting/laravel-sortable

Composer 安装命令:

composer require akaunting/laravel-sortable

包简介

Sortable behavior package for Laravel

README 文档

README

Downloads Tests StyleCI License

This package allows you to add sortable behavior to models and views. It ships with a trait where you can set the sortable fields and a blade directive to generate table headers automatically.

Getting Started

1. Install

Run the following command:

composer require akaunting/laravel-sortable

2. Publish

Publish configuration

php artisan vendor:publish --tag=sortable

3. Configure

You can change the column sorting settings of your app from config/sortable.php file

Usage

All you have to do is use the Sortable trait inside your model and define the $sortable fields.

use Akaunting\Sortable\Traits\Sortable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Sortable;
    ...

    public $sortable = [
        'id',
        'title',
        'author',
        'created_at',
    ];
    ...
}

If you don't define the $sortable array, the Scheme::hasColumn() function is used which runs an extra database query.

Scope

The trait adds a sortable scope to the model so you can use it just before paginate:

public function index()
{
    $posts = Post::query()->sortable()->paginate(10);

    return view('posts.index')->with(['posts' => $posts]);
}

You can set also default sorting field which will be applied when URL is empty.

$posts = $post->sortable(['author'])->paginate(10); // $post->orderBy('posts.author', 'asc')

$posts = $post->sortable(['title'])->paginate(10); // $post->orderBy('posts.title', 'asc')

$posts = $post->sortable(['title' => 'desc'])->paginate(10); // $post->orderBy('posts.title', 'desc')

Blade Directive

There is also a blade directive for you to create sortable links in your views:

@sortablelink('title', trans('general.title'), ['parameter' => 'smile'],  ['rel' => 'nofollow'])

The first parameter is the column in database. The second one is displayed inside the anchor tag. The third one is an array(), and it sets the default (GET) query string. The fourth one is also an array() for additional anchor-tag attributes. You can use a custom URL as 'href' attribute in the fourth parameter, which will append the query string.

Only the first parameter is required.

Examples:

@sortablelink('title')
@sortablelink('title', trans('general.title'))
@sortablelink('title', trans('general.title'), ['filter' => 'active, visible'])
@sortablelink('title', trans('general.title'), ['filter' => 'active, visible'], ['class' => 'btn btn-success', 'rel' => 'nofollow', 'href' => route('posts.index')])

Icon Set

You can use any icon set you want. Just change the icons.wrapper from the config file accordingly. By default, it uses Font Awesome.

Blade Component

Same as the directive, there is also a blade component for you to create sortable links in your views:

<x-sortablelink column="title" title="{{ trans('general.title') }}" :query="['parameter' => 'smile']"  :arguments="['rel' => 'nofollow']" />

Sorting Relationships

The package supports HasOne and BelongsTo relational sorting:

class Post extends Model
{
    use Sortable;
    ...

    protected $fillable = [
        'title',
        'author_id',
        'body',
    ];

    public $sortable = [
        'id',
        'title',
        'author',
        'created_at',
        'updated_at',
    ];

    /**
    * Get the author associated with the post.
    */
    public function author()
    {
        return $this->hasOne(\App\Models\Author::class);
    }
    ...
}

And you can use the relation in views:

// resources/views/posts/index.blade.php

@sortablelink('title', trans('general.title'))
@sortablelink('author.name', trans('general.author'))

Note: In case there is a self-referencing model (like comments, categories etc.); parent table will be aliased with parent_ string.

Advanced Relation

You can also extend the relation sorting feature by creating a function with Sortable suffix. There you're free to write your own queries and apply orderBy() manually:

class User extends Model
{
    use Sortable;
    ...

    public $sortable = [
        'name',
        'address',
    ];

    public function addressSortable($query, $direction)
    {
        return $query->join('user_details', 'users.id', '=', 'user_details.user_id')
                    ->orderBy('address', $direction)
                    ->select('users.*');
    }
    ...

The usage in controller and view remains the same.

Aliasing

You can declare the $sortableAs array in your model and use it to alias (bypass column exists check), and ignore prefixing with table:

public $sortableAs = [
    'nick_name',
];

In controller

$users = $user->select(['name as nick_name'])->sortable(['nick_name'])->paginate(10);

In view

@sortablelink('nick_name', 'nick')

It's very useful when you want to sort results using withCount().

Changelog

Please see Releases for more information what has changed recently.

Contributing

Pull requests are more than welcome. You must follow the PSR coding standards.

Security

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

Credits

License

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

akaunting/laravel-sortable 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 193.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 4
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-30