PHPackages                             plokko/querable-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. [Database &amp; ORM](/categories/database)
4. /
5. plokko/querable-resource

ActiveLibrary[Database &amp; ORM](/categories/database)

plokko/querable-resource
========================

Ajax-querable resource for Laravel

0.2.1(3y ago)0255PHPPHP &gt;=7.0.0

Since Feb 25Pushed 3y ago1 watchersCompare

[ Source](https://github.com/plokko/querable-resource)[ Packagist](https://packagist.org/packages/plokko/querable-resource)[ RSS](/packages/plokko-querable-resource/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (7)Dependencies (1)Versions (8)Used By (0)

QuerableResource
================

[](#querableresource)

A Laravel class that helps querying(paging and filtering) and returning Eloquent resources from Ajax.

Supports Laravel 5.5 Resources.

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

[](#installation)

Install it throught composer `composer require plokko/querable-resource`

Quick start
-----------

[](#quick-start)

First of all you need to extend the `Plokko\QuerableResource\QuerableResource` class implementing the *getQuery* method that returns the base query:

```
 class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {

      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
  }
```

Next we're going to instantiate it and return it to the request

```
Route::get('/test',function(){
    $qr = new TestQuerableResource();
    return $qr;
});
```

The page should return a basic resource with all the results

### Pagination

[](#pagination)

To enable pagination edit the *$paginate* protected propriety or via the *pagination()* method setting it to the wanted page size or to *null* to disable it.

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $paginate = 30,//30 items per page
        /**
         * Allowed client-defined paginations,
         *      if null no client pagination is allowed
         *      if int set the maxium page size allowed
         *      if array only the page sizes listed in the array are allowed
         * @var int|array|null
         */
        $paginations = 100;
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
}
```

The propriety *$paginations* specify the user-selectable available values, if the value is `null` only *$paginate* user values will be discarted in favor of *$paginate*, if an int value is specified it will be set as the maxium value (in this case the user could select pagination up to 100 with default of 30 items per page); if an array (of integers) is specified the user value must be contained in the array or *$paginate* value will be used (ex. `[10,20,30,50]` )

```
Route::get('/test',function(){
    $qr = new TestQuerableResource();
    $qr->paginate(10); // Set pagination to 10 items per page
    return $qr;
});
```

### Filtering results

[](#filtering-results)

To filter results extend the protected propriety *$filteredFields* specifying what fields you want to filter

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = ['name','email']; // enable filtering for name and email columns
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
}
```

You do not need to pass anything to the resource, the filtering is automatic and transparent.

```
Route::get('/test',function(){
    $qr = new TestQuerableResource();
    return $qr;
});
```

By default the comparison is done with the *LIKE 'value%'*; for example the page `/test?name=a` will search all the user with a name starting with 'a'.

#### Alias

[](#alias)

If you want to use a query parameter different from the field you can specify the alias as key for the field:

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = [
		'n' => 'name', // query parameter n to field name
		'mail' => 'email', // query parameter mail to field email
	];
	//....
}
```

#### Advanced filtering options

[](#advanced-filtering-options)

Instead of the field name you can specify an array of filtering options like

- *field* - string (required) - corresponding field name, required
- *type* - string - type of comparaison (ex. '=','like','&gt;','&gt;=',...)

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = [
		'n' => [ //an can be used
			'field'=>'name',
			'type'=>'like',
		],
		[
			'field'=>'email', // If no alias is specified field will be used as query parameter
			//comparaison type is optional
		]
	];
	//....
}
```

#### Changing query field name

[](#changing-query-field-name)

If you want to group all filtering field in an input group, for example filter\[filed\_name\], you need to edit the protected propriety *$filterQueryParameter*

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = ['name','email'], // enable filtering for name and email columns
        $filterQueryParameter='filter';
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
}
```

The url of the last example will now be `/test?filter[name]=a`

#### Define filtering rules

[](#define-filtering-rules)

#### Custom filtering function

[](#custom-filtering-function)

If your query needs a more fine-tuning you can override the default *filter* function and specify your custom filtering function:

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
	// No need to specify $filteredFields, we're using a custom filtering function

	//Override the default filtering function
    protected function filter(Builder &$query,array $filterValues){
		if(array_key_exists('name',$filterValues)){
			$query->where('name','LIKE','%'.$filterValues['name'].'%');//apply your filtering
		}
		//....
	}

	protected function getQuery(): Illuminate\Database\Eloquent\Builder
	{
		return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
	}
}
```

Don't worry, pagination and query field name will also be automatically applied to your filtering, no need to implement them!

### Returning custom resource

[](#returning-custom-resource)

If you want to filter the results with a custom resource you can do it by simply specifying the name of your Resource class in the protected field *$useResource*:

```
class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
  protected
   $useResource = \App\Http\Resources\UserResource::class;
  protected function getQuery(): Illuminate\Database\Eloquent\Builder
  {
	  return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
  }
}
```

The class will now return the specified resource (as a collection); for further detail on resources see Laravel 5.5 API resources doc.

Javascript integration
----------------------

[](#javascript-integration)

This plugin contains some Javascript assets to help integrate with the php counterpart.

This plugin includes two main helper:

- *ResourceQuery* For building an Ajax request to the php plugin counterpart
- *QuerableResult* Wrapper of *ResourceQuery* that returns a querable result instead of a query, usefull especially in Vue.js applications

```
// Import from the /vendor folder, this path is related to /resources/assets/js/components/
// Import QuerableResult and ResourceQuery (optional)
import QuerableResult,{ResourceQuery} from "../../../../vendor/plokko/querable-resource/assets/js/QuerableResult";
// Or just ResourceQuery
//import ResourceQuery from "../../../../vendor/plokko/querable-resource/assets/js/ResourceQuery";
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~291 days

Recently: every ~433 days

Total

7

Last Release

1255d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/755cf9f83a0a5af9141085f8961276809fc04356f3aee061ac7b72723a4e6290?d=identicon)[plokko](/maintainers/plokko)

---

Tags

laraveleloquentfilterresourcepaginate

### Embed Badge

![Health badge](/badges/plokko-querable-resource/health.svg)

```
[![Health](https://phpackages.com/badges/plokko-querable-resource/health.svg)](https://phpackages.com/packages/plokko-querable-resource)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

450191.6k1](/packages/mehdi-fathi-eloquent-filter)[jedrzej/searchable

Searchable trait for Laravel's Eloquent models - filter your models using request parameters

127259.1k5](/packages/jedrzej-searchable)[czim/laravel-filter

Filter for Laravel Eloquent queries, with support for modular filter building

8973.0k3](/packages/czim-laravel-filter)[omalizadeh/laravel-query-filter

A laravel package for resource filtering via request query string

163.0k](/packages/omalizadeh-laravel-query-filter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
