PHPackages                             liveintent/laravel-resource-search - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. liveintent/laravel-resource-search

AbandonedArchivedLibrary

liveintent/laravel-resource-search
==================================

Utility for easily setting up search requests for Laravel resources

v0.1.0(3y ago)02.9kPHPPHP ^8.0|^8.1

Since Aug 31Pushed 3y ago6 watchersCompare

[ Source](https://github.com/LiveIntent/laravel-resource-search)[ Packagist](https://packagist.org/packages/liveintent/laravel-resource-search)[ Docs](https://github.com/liveintent/laravel-resource-search)[ RSS](/packages/liveintent-laravel-resource-search/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (7)Versions (5)Used By (0)

🔍 Laravel Resource Search
=========================

[](#-laravel-resource-search)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6ba2894e5e4c72221ff8ef49dce45f450efd0ad19e6c7c1d811fbf7f6e42dcbf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c697665696e74656e742f6c61726176656c2d7265736f757263652d7365617263682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/liveintent/laravel-resource-search)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b0e0d656cc0f9b3e87b16b632f6146268da68b13c439b8dc229c81a473b48752/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c697665696e74656e742f6c61726176656c2d7265736f757263652d7365617263682f746573742d7068703f6c6162656c3d7465737473)](https://github.com/liveintent/laravel-resource-search/actions?query=workflow%3Atest-php+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/6188288312e7648755844b1ca08a3d01646f30cc73380c5f4891739431fc52ea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c697665696e74656e742f6c61726176656c2d7265736f757263652d7365617263682f6c696e742d7068703f6c6162656c3d636f64652532307374796c65)](https://github.com/liveintent/laravel-resource-search/actions?query=workflow%3Alint-php+branch%3Amain)

This package allows you to easily and safely provide comprehensive filtering and sorting capabilities to your Laravel endpoints by simply defining which fields should be exposed in the corresponding Eloquent API Resource object.

The package is heavily inspired by the [Spatie Query Builder](https://github.com/spatie/laravel-query-builder/tree/main) and [Laravel Orion](https://tailflow.github.io/laravel-orion-docs/).

Basic usage
-----------

[](#basic-usage)

### Basic functionality is availble via query parameters in GET requests

[](#basic-functionality-is-availble-via-query-parameters-in-get-requests)

```
 // find employees where `name` = "Michael"
GET /employees?filter[name]=Michael

// find employees where `name` like "Michael%"
GET /employees?filter[name]=Michael*

// find employees where `name` like "Michael%" OR `name` like "Dwight"
GET /employees?filter[name]=Michael*,Dwight

// find employees where `name` =  "Michael" AND `email` like "%@dundermifflin.com"
GET /employees?filter[name]=Michael&filter[email]=*@dundermifflin.com

// find employees where `email` like "%@dundermifflin.com" order by `hired_at`
GET /employees?filter[email]=*@dundermifflin.com&sort=hired_at

// find employees where `email` like "%@dundermifflin.com" order by `hired_at` desc
GET /employees?filter[email]=*@dundermifflin.com&sort=-hired_at

// find the two most recently hired employees where any searchable field contains "stamford"
GET /employees?q=stamford&sort=-hired_at&page[size]=2
```

[Read more about basic search capabilities like: filters, pagination, search, sort...](/docs/README.md)

### Extended functionality is availble via body payload in POST requests

[](#extended-functionality-is-availble-via-body-payload-in-post-requests)

```
// POST /employees/search?page[size]=10
{
    "scopes" : [
        {"name" : "active"},
        {"name" : "whereBranch", "parameters" : ["scranton"]}
    ],
    "filters" : [
        {"field" : "hired_at", "operator" : ">=", "value" : "2001-01-01"},
        {"type" : "or", "field" : "name", "operator" : "in", "value" : ["Ed Truck", "Tod Packer"]}
    ],
    "search" : {
        "value" : "manager"
    },
    "sort" : [
        {"field" : "dundie_count", "direction" : "desc"},
        {"field" : "dundie.name", "direction" : "asc"}
    ]
}
```

[Read more about advanced search capabilities like: scopes, filters, pagination, search, sort...](/docs/README.md)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require liveintent/laravel-resource-search
```

The package will automatically register its service provider.

You can optionally publish the config file with:

```
php artisan vendor:publish --provider="LiveIntent\LaravelResourceSearch\LaravelResourceSearchServiceProvider" --tag="resource-search-config"

```

### Adding searchable resources to your API

[](#adding-searchable-resources-to-your-api)

Laravel's [Eloquent API Resources](https://laravel.com/docs/master/eloquent-resources) serve as a transformation layer that sits betweeen your Eloquent models and the JSON responses that are actually returned to your application's usesr. For this reason, we think this is a great place to also define the capabilities your users should have when listing and searching those resources.

To transform any existing Eloquent resource into a Searchable Eloquent resource, you should have your resource use the `Searchable` trait provided by this package and instruct the resource that it `implements` the `SearchableResource` interface. Finally, you'll need to define a `$model` property on the resource which indicates which underlying Eloquent model should be used for the base query.

```
