firevel/filterable

A simple trait for Laravel Eloquent models that allows you to easily filter your queries.

0.0.10 2024-10-03 23:12 UTC

This package is auto-updated.

Last update: 2024-11-03 23:16:00 UTC


README

A simple trait for Laravel Eloquent models that allows you to easily filter your queries.

Installation

composer require firevel/filterable

Setup

  1. Add the Filterable trait to your Eloquent model.
  2. Define the $filterable property on your model to specify which attributes can be filtered and their types.
use Firevel\Filterable\Filterable;

class User extends Model
{
    use Filterable;

    protected $filterable = [
        'user_id' => 'id',
        'name' => 'string',
        'created_at' => 'datetime',
        // ... other attributes ...
    ];
}

Allowed Filter Types

  • integer
  • date
  • datetime
  • id
  • string
  • relationship
  • boolean
  • json
  • array

Usage

Use the filter() scope on your Eloquent query to apply filters.

// Fetch users with user_id equal to 5
$users = User::filter(['user_id' => ['=' => 5]])->get();

// Fetch users created after a certain date
$users = User::filter(['created_at' => ['>' => '2023-01-01']])->get();

// ... other filter combinations ...