承接 hamidreza-mozhdeh/filter-by-query-string 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱: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