Easily build Eloquent queries from API requests.

v1.0 2021-11-08 19:37 UTC

This package is auto-updated.

Last update: 2022-01-09 08:08:00 UTC


README

Larapi

Larapi

Filter your Laravel API like a Pro

Tests PSR-12 Standard

Introduction

This package allows you to filter Eloquent models via API using querystring requests.

Getting Started

  • Install Vendor:
composer require andreapollastri/larapi
  • Larapi support two controllers resources methods, index and show. Put this code into your controller methods:
    public function index(Request $request, Larapi $larapi)
    {
      $data = $larapi->filterIndex('\App\Models\<YOURMODEL>', $request)

      if(!$data) {
        abort(422);
      }

      return response()->json($data);
    }

    public function show(Request $request, Larapi $larapi)
    {
      $data = $larapi->filterShow('\App\Models\<YOURMODEL>', $request);

      if(!$data) {
        abort(422);
      }

      return response()->json($data);
    }
  • If response is NULL it means that there is a Query issue, check your parameters (and manage Bad Request error).

  • Remember to use {id} parameter for show routes

  • Model hidden fields are "not allowed" in query filters

  • You can define a set of optional "base" custom "where" filters using $customFilters parameters, e.g.:

    $data = $larapi->filterIndex('\App\Models\News', $request, ['is_online' => true]);
  • You can define a Laravel native cache layer, e.g.:
    $data = Cache::remember(sha1(url()->full()), 3600, function () use ($larapi, $request) {
      return $larapi->filterIndex('\App\Models\News', $request);
    });



Usage


Index Filters

Parameter Description Example
where Field equal parameter ?where[firstname]=John&where[lastname]=Doe
whereNot Field not equal parameter ?whereNot[id]=123
like Field like %value% parameter ?like[city]=erlin
startsWith Field startsWith value% parameter ?startsWith[city]=Ber
endsWith Field endsWith %value parameter ?endsWith[city]=in
higher Field higher than parameter ?higher[age]=25
lower Field lower than parameter ?lower[distance]=250
order Fields order parameter (comma separator, pipe asc or desc to define direction - asc by default) ?order=price,brand
rand Get random records ?rand=true
only Select only fields parameter (comma separator) ?only=firstname,lastname
with With relations parameter (comma separator) ?with=categories,author,tags
has WhereHas Related Subquery using table, field and value ?has[comments][author_id]=7
hasLike WhereHas Like Related Subquery using table, field and value ?haslike[products][description]=camping
paginate Number of record in paginate option ?paginate=10
page Number of current page in pagination (only in paginate mode) ?page=3
limit If you are not using paginate option, you can limit the result set using limit parameter ?limit=25

Show Filters

Parameter Description Example
only Select only fields parameter (comma separator) ?only=firstname,lastname
with With relations parameter (comma separator) ?with=categories,author,tags



Security Vulnerabilities and Bugs

If you discover any security vulnerability or any bug within larapi, please open an issue.

Contributing

Thank you for considering contributing to this project!

Licence

Larapi is open-source software licensed under the MIT license.

Enjoy larapi ;)