PHPackages                             mobileka/scope-applicator-laravel - 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. [Search &amp; Filtering](/categories/search)
4. /
5. mobileka/scope-applicator-laravel

ActiveLibrary[Search &amp; Filtering](/categories/search)

mobileka/scope-applicator-laravel
=================================

Scope Applicator bindings for Laravel framework.

1.1.2(9y ago)0811MITPHP

Since Jul 4Pushed 9y agoCompare

[ Source](https://github.com/mobileka/scope-applicator-laravel)[ Packagist](https://packagist.org/packages/mobileka/scope-applicator-laravel)[ RSS](/packages/mobileka-scope-applicator-laravel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (8)Versions (8)Used By (0)

[![Build Status](https://camo.githubusercontent.com/b87e1bbf3cd742bc98dbc87ac84580ce45579cfbff7bd7ecafced12f675bc3aa/68747470733a2f2f7472617669732d63692e6f72672f6d6f62696c656b612f73636f70652d6170706c696361746f722d6c61726176656c2e737667)](https://travis-ci.org/mobileka/scope-applicator-laravel)[![Code Climate](https://camo.githubusercontent.com/6b994043ffd1121be732da5923664aad991e94cd2296fd64760285938c41a32a/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6f62696c656b612f73636f70652d6170706c696361746f722d6c61726176656c2e737667)](https://codeclimate.com/github/mobileka/scope-applicator-laravel)[![Coverage Status](https://camo.githubusercontent.com/1bd1c07a7c1d2d7ac58fc4127be985034a09bb19062402fea9955eade6bd25b6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d6f62696c656b612f73636f70652d6170706c696361746f722d6c61726176656c2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/mobileka/scope-applicator-laravel?branch=master)

*If you're looking for a version which works with Laravel 4.x.x and 5.0.x, click [here](https://github.com/mobileka/scope-applicator-laravel/tree/laravel_4_and_5.0).*

[ScopeApplicator](https://github.com/mobileka/scope-applicator) brings an elegant way of sorting and filtering data to your Laravel projects.

- [Overview](#overview)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage (with Models)](#usage-with-models)
- [A better usage scenario (with Repositories)](#a-better-usage-scenario-with-repositories)
- [Contributing](#contributing)
- [License](#license)

Overview
--------

[](#overview)

ScopeApplicator is an easy and logical way to achieve something like this:

`/posts` – returns a list of all posts

`/posts?recent` – returns only recent posts

`/posts?author_id=5` – returns posts belonging to an author with an `id=5`

`/posts?author_id=5&order_by_title=desc&status=active` – returns only active posts belonging to an author with an `id=5` and sorts them by a title in a descending order

Requirements
------------

[](#requirements)

Laravel ~5.1

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

[](#installation)

`composer require mobileka/scope-applicator-laravel 1.1.*`

Usage (with Models)
-------------------

[](#usage-with-models)

> Make sure you are familiar with Laravel's [query scopes](http://laravel.com/docs/eloquent#query-scopes) before you dive in

Let's learn by example. First of all, we'll implement an `author_id` filter for `posts` table.

> Please note that this is going to be a basic example and it's not the most optimal way of doing things ;)

These are steps required to achieve this:

1. Create a basic `PostController` which outputs a list of posts when you hit `/posts` route
2. Create a `userId` scope in the `Post` model (and it has to extend the `Mobileka\ScopeApplicator\Laravel\Model` class)
3. Tell ScopeApplicator that this scope is available and give it an alias
4. Visit `/posts?author_id=1` and enjoy the result

Ok, let's cover these step by step.

— The `PostController`:

```
