Giter VIP home page Giter VIP logo

laravel-rest-filters's Introduction

Laravel Rest Filters

The Missing RESTFul API AutoFilters Provider for Laravel.

Table of Contents

Quick Start

Install the Package.

composer require itsrennyman/laravel-rest-filters

Set Up Filtering

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    return User::withRestFilters()->get();
}

Start Filtering

In order to Filter, pass the parameters in the URI Querystring.

// http://localhost:8000/api/[email protected]

[
  {
    "id": 1,
    "name": "Victoria Shanahan",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

And Done. So Sexy So Fresh!

Basic Filters

Single Field Where

// http://localhost:8000/api/[email protected]

[
  {
    "id": 1,
    "name": "Victoria Shanahan",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

Single Field Where with Multiple Values

// http://localhost:8000/api/[email protected],[email protected]

[
  {
    "id": 1,
    "name": "Victoria Shanahan",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 2,
    "name": "Lenore Stroman II",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

Multiple Fields Where

Multiple fields are joined as multiple AND wheres.

// http://localhost:8000/api/[email protected]&id=3

[
  {
    "id": 3,
    "name": "Orval Lockman Sr.",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

Field Sorting

You can use the minus operator in front of the field, in order to do the DESC sorting. Multiple order by are supported, use the comma as separator.

// http://localhost:8000/api/users?sort=-id

[
  {
    "id": 10,
    "name": "Dr. Jason Russel I",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 9,
    "name": "Mrs. Yasmine Wintheiser",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 8,
    "name": "Garett Botsford Sr.",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 7,
    "name": "Karli Ondricka",
    "email": "[email protected]",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

Field Selecting

// http://localhost:8000/api/users?fields=id,name

[
  {
    "id": 1,
    "name": "Mikayla Stanton"
  },
  {
    "id": 2,
    "name": "Mr. Newell Raynor Jr."
  },
  {
    "id": 3,
    "name": "Theodora O'Conner"
  }
]

Advanced Filters

Greater Than

// http://localhost:8000/api/users?id=gt:4

[
  {
    "id": 5,
    "name": "Jazmyne Lang",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 6,
    "name": "Matteo Feest",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 7,
    "name": "Tabitha Wiegand Jr.",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

Greater Than Equal

// http://localhost:8000/api/users?id=gte:4

[
  {
    "id": 4,
    "name": "Eleanora Harris",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 5,
    "name": "Jazmyne Lang",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 6,
    "name": "Matteo Feest",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 7,
    "name": "Tabitha Wiegand Jr.",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

Less Than

// http://localhost:8000/api/users?id=lt:4

[
  {
    "id": 1,
    "name": "Miss Damaris Medhurst PhD",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 2,
    "name": "Ms. Sarina Volkman III",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 3,
    "name": "Prof. Asha Hane",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

Less Than Equal

// http://localhost:8000/api/users?id=lte:4

[
  {
    "id": 1,
    "name": "Miss Damaris Medhurst PhD",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 2,
    "name": "Ms. Sarina Volkman III",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 3,
    "name": "Prof. Asha Hane",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 4,
    "name": "Eleanora Harris",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

Like & iLike

NB: Currently iLike is not supported on MySQL.

With the like attribute, the query search the full value passed as parameter. If you need to do search, be sure to add the percentage '%' to your values.

// http://localhost:8000/api/users?name=like:%Mr%

[
  {
    "id": 17,
    "name": "Mrs. Verlie Cummerata",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 19,
    "name": "Mr. Maynard Conn PhD",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 26,
    "name": "Mrs. Marcelle Cole IV",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 68,
    "name": "Mrs. Helga Hansen",
    "email": "[email protected]",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:32.000000Z",
    "updated_at": "2020-11-03T15:43:32.000000Z"
  }
]

Ban Filters & Attributes

// TODO

Contribution

If you have ideas, or improvements for this Project, please open a Pull Request.

laravel-rest-filters's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.