定制 zihad/pilter 二次开发

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

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

zihad/pilter

最新稳定版本:0.1.1

Composer 安装命令:

composer require zihad/pilter

包简介

A filter package to help developers to attach filter queries to their query builder.

README 文档

README

Pilter is a Laravel package designed to simplify and organize filter and sorting queries in your Laravel applications. By separating these operations into dedicated classes, Pilter helps make your code cleaner, more readable, and easier to maintain.

Installation

You can install the package via Composer:

composer require zihad/pilter

Usage/Examples

Create a new Filter class PostFilter

php artisan make:filter Post

namespace App\Filters;

use Zihad\Pilter\Filters\Filter;

class PostFilter extends Filter
{
    //
}

You can use the Filter class like this:

$posts = (new PostFilter(Post::query(), ['title' => 'Post 1', 'sort' => '-title']))
    ->filter()
    ->sort()
    ->get();

Or

namespace App\Models;

use Zihad\Pilter\Traits\Filterable;

// Use Filterable trait in model
class Post extends Model
{
    use Filterable;
}


$posts = Post::query()
    ->applyQuery(['title' => 'Post 1', 'sort' => '-title']), PostFilter::class)
    ->get();

You can filter and sort by defining separate methods in PostFilter.php

// Filter method
protected function title($title)
{
    $this->getQuery()->where('title', $title);
}
// Sort method
protected function sortTitle($order)
{
    $this->getQuery()->orderBy('title', $order);
}

Or you can do the basic filters and sorting by adding then in filterable and sortable attributes in PostFilter.php

protected array $filterableFields = ['title', 'category', 'slug'];
protected array $sortableFields = ['title', 'category', 'slug'];

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-04