PHPackages                             bayareawebpro/searchable-resource - 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. [API Development](/categories/api)
4. /
5. bayareawebpro/searchable-resource

AbandonedArchivedLibrary[API Development](/categories/api)

bayareawebpro/searchable-resource
=================================

Searchable Resource Builder for Laravel

v1.0.29(4y ago)4138MITPHPPHP ^7.4|^8.0

Since Feb 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/bayareawebpro/searchable-resource)[ Packagist](https://packagist.org/packages/bayareawebpro/searchable-resource)[ Docs](https://github.com/bayareawebpro/searchable-resource)[ RSS](/packages/bayareawebpro-searchable-resource/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (5)Versions (32)Used By (0)

Laravel Searchable Resource Builder
===================================

[](#laravel-searchable-resource-builder)

[![CI](https://github.com/bayareawebpro/searchable-resource/workflows/ci/badge.svg)](https://github.com/bayareawebpro/searchable-resource/workflows/ci/badge.svg)[![Coverage](https://camo.githubusercontent.com/05be48145ce122de1d9927d156bd7c0a2465ede7b6c1791fb26c478250903d2b/68747470733a2f2f636f6465636f762e696f2f67682f6261796172656177656270726f2f73656172636861626c652d7265736f757263652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://camo.githubusercontent.com/05be48145ce122de1d9927d156bd7c0a2465ede7b6c1791fb26c478250903d2b/68747470733a2f2f636f6465636f762e696f2f67682f6261796172656177656270726f2f73656172636861626c652d7265736f757263652f6272616e63682f6d61737465722f67726170682f62616467652e737667)[![Downloads](https://camo.githubusercontent.com/cc0020a74115623f130ae9f5dc2f64c7fcf1bfd9d48926ea2e816c9590e95a23/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6261796172656177656270726f2f73656172636861626c652d7265736f757263652e737667)](https://camo.githubusercontent.com/cc0020a74115623f130ae9f5dc2f64c7fcf1bfd9d48926ea2e816c9590e95a23/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6261796172656177656270726f2f73656172636861626c652d7265736f757263652e737667)[![Version](https://camo.githubusercontent.com/1ace622fbf81574defaba0565a04a715c8b35a0d6c6c05279155f401ad373bff/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6261796172656177656270726f2f73656172636861626c652d7265736f757263652e737667)](https://camo.githubusercontent.com/1ace622fbf81574defaba0565a04a715c8b35a0d6c6c05279155f401ad373bff/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6261796172656177656270726f2f73656172636861626c652d7265736f757263652e737667)[![MIT](https://camo.githubusercontent.com/b60ae987297da06088ea8e815265e37e4627c6f6485d19a3dcb963b576141b28/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d737563636573732e737667)](https://camo.githubusercontent.com/b60ae987297da06088ea8e815265e37e4627c6f6485d19a3dcb963b576141b28/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d737563636573732e737667)

Searchable Resource Builder is an abstraction for building searchable resource responses in Laravel applications. Extract query logic into reusable chunks while using a fluent builder interface for dealing with searchable / filterable / sortable requests and JSON / API Resources.

```
composer require bayareawebpro/searchable-resource
```

### Basic Usage

[](#basic-usage)

SearchableResources implement the `Responsable` interface which allows them to be returned from controllers easily. It can also be used with blade.

The `make` method accepts instances of Eloquent Builder.

```
SearchableResource::make(User::query());
```

### Ordering and Sorting

[](#ordering-and-sorting)

You can specify as many orderable columns as you wish.

```
SearchableResource::make(User::query())
	->orderable(['name', 'email'])
	->orderBy('name')
	->sort('desc')
	->paginate(16);
```

The default settings:

- order\_by ID
- sort DESC

---

### Full Example

[](#full-example)

```
use App\User;
use App\Queries\UserSearch;
use App\Queries\RoleFilter;
use App\Http\Resources\UserResource;
use BayAreaWebPro\SearchableResource\SearchableResource;
use BayAreaWebPro\SearchableResource\SearchableBuilder;

SearchableResource::make(User::query())
    ->resource(UserResource::class)
    ->queries([
        UserSearch::class,
        RoleFilter::class
    ])
    ->orderable([
        'id', 'name', 'email', 'role',
        'created_at', 'updated_at',
    ])
    ->appendable([
        'created_for_humans',
        'updated_for_humans',
    ])
    ->select([
        'id',
        'name', 'email', 'role',
        'created_at', 'updated_at',
    ])
    ->rules([
       'my_filter_key' => 'required|string'
    ])
    ->params([
       'my_filter_key' => 'my_default'
    ])
    ->options([
       'my_filter_key' => ['my_default', 'option2', 'option3']
    ])
    ->with([
        'my_key' => true
    ])
    ->when(true, fn(SearchableBuilder $builder)=>$builder
        ->with([
            'my_key' => false
        ])
    )
    ->orderBy('updated_at')
    ->sort('desc')
    ->paginate(16)
    ->labeled();
```

---

### Blade / View Example

[](#blade--view-example)

Execute the query and return a view with the items and options.

```
public function index()
{
    $resource = SearchableResource::make(User::query())
        ->query(Users::make())
        ->orderable(['name', 'email'])
        ->orderBy('name')
        ->sort('desc')
        ->paginate(5)
        ->execute()
    ;
    return view('users.index', [
        'items' =>$resource->getItems(),
        'search' =>$resource->getSearch(),
        'order_by' =>$resource->getOrderBy(),
        'per_page' =>$resource->getPerPage(),
        'options' =>$resource->getOptions(),
        'sort' =>$resource->getSort(),
    ]);
}
```

```

```

---

### JSON Resources

[](#json-resources)

SearchableResources are generic JsonResources by default. You can easily specify which resource class should be used to map your models when building the response.

> Must extend `JsonResource`.

```
SearchableResource::make(User::query())->resource(UserResource::class);
```

---

### Invokable Queries

[](#invokable-queries)

Queries are expressed as invokable classes that extend the `AbstractQuery` class which contains logic per request field. Queries can apply to multiple attributes/columns as well as multiple inputs for `orWhere` clauses.

`php artisan make:searchable NameQuery`

The following is an example of a generic name query:

```
