j-dexx/eloquent-ransack

Easily filter eloquent models

v1.0.2 2020-09-09 08:23 UTC

This package is auto-updated.

Last update: 2024-04-09 17:14:59 UTC


README

Inspired by the ransack gem eloquent ransack's goal is to provide a simplistic filtering method on eloquent models.

Usage

The Ransackable trait provides a ransack scope that you pass an array of input to. All filters should be of the form column_name_predicate where predicate is one of the options listed in the table below.

Available Filtering Types

predicate example sql
eq name_eq where "name" = ?
not_eq name_not_eq where "name" != ?
cont name_cont where "name" LIKE ?
in category_id_in where "category_id" in (?)
not_in category_id_not_in where "category_id" not in (?)
lt date_lt where "date" < ?
lte date_lte where "date" <= ?
gt date_gt where "date" > ?
gte date_gte where "date" >= ?

Example

Eloquent Model

use Jdexx\EloquentRansack\Ransackable;

class Post
{
  use Ransackable;
}

Form

<form>
  <input type="text" name="name_eq" />
</form>

Controller

class PostsController
{
  public function index(Request $request)
  {
    $params = $request->all();
    $posts = Post::ransack($params)->get();
  }
}