hamidreza-mozhdeh/filter-by-query-string 问题修复 & 功能扩展

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

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

hamidreza-mozhdeh/filter-by-query-string

最新稳定版本:v1.0.3

Composer 安装命令:

composer require hamidreza-mozhdeh/filter-by-query-string

包简介

Filter Laravel Eloquent results by URL query strings and Eloqunet's scopes.

README 文档

README

Please give me a star if you like it.

About Filter by query string

With this package, you can simply use Eloquent scopes to filter the model result by using URL query strings. The advantage of this repository is that you can reuse your local scopes in your business logic (Controllers, Services, etc).

  • It's safe
  • Reusable local scopes
  • Easy to implement
  • Customizable

How to install

Run composer require hamidreza-mozhdeh/filter-by-query-string. It will add a FilterByQueryString trait to your project.

How to use

  • Add the trait to your models use FilterByQueryString;.

Category Model:

  class Category extends Model
  {
    use CategoryScopesTrait;
    use FilterByQueryString;
  }
  • In your controller simply pass the Form Request to the Model:
    • Example: $categories = Category::filter($request);
    • Please note you have to type hint the Request class.
    • Important: First of all is better to have a From Request for each of your actions or methods (optional). If not, you have to change requestMethod to input which is not recommended.

CategoryController:

class CategoryController extends Controller
{
    public function index(CategoryRequest $request)
    {
        return Category::filter($request)->get();
    }

CategoryRequest:

class CategoryRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['sometimes', 'string', 'min:3', 'max:254'], // A single filter

            // Use Associative example
            'date-between' => ['sometimes', 'array'], // A filter with two values
            'date-between.from' => ['sometimes', 'date'], // Associative array
            'date-between.to' => ['sometimes', 'date'], // Associative array
             
            // Or Indexed example
            'date-between' => ['sometimes', 'array'], // A filter with two values
            'date-between.*' => ['sometimes', 'date'], // Indexed array
        ];
    }
}
  • Define your scopes to use the filters
    • It's better to have a directory and traits for your models in some where like App\Models\Traits\Scopes\CategoryScopesTrait.

CategoryScopesTrait:

trait CategoryScopesTrait
{
    public function scopeName(Builder $query, string $name): Builder
    {
        return $query->where('name', 'like', "%{$name}%");
    }

    public function scopeDateBetween(Builder $query, string $from, string $to): Builder
    {
        return $query->whereBetween('created_at', [$from, $to]);
    }
}
  • To filter the categories' names for example ossw use:
    http://0.0.0.0/api/categories?name=ossw
  • To filter with dates use:
    Indexed array: http://0.0.0.0/api/categories?date-between[]=2023-11-23&date-between[]=2023-11-25
    Associative array: http://0.0.0.0/api/categories?date-between[from]=2023-11-23&date-between[to]=2023-11-25

Customization

  $categories = Category::filter(
    request: $request,
    only: ['name'], // Only accept this filter.
    except: ['date-between'], // Do not accept the `date-between` method.
    prefix: 'filters', // Add `filters` as array prefix.
    requestMethod: 'input' // The default get method is validated but you can choose different one.
  );

With prefix: http://0.0.0.0/api/categories?filters[name]=ossw

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-12-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固