Laravel: Making secure filters for Eloquent Models
Tags: laravel, php, eloquent, filter
Published: 2019-07-22 15:15:40

---
title:Laravel: Making secure filters for Eloquent Models
published: true
description: 
tags: #Laravel #PHP #Eloquent #Filter
cover_image: https://images.unsplash.com/photo-1484043937869-a468066a4fbd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80
---
## Introduction
In the previous post, I introduced a package that allowed you to filter for your Eloquent models in Laravel.As you can see [here](https://dev.to/mohammadfouladgar/making-the-advanced-search-query-with-eloquent-builder-in-laravel-30g3).
> The latest version of this package is available in Github repository: [v1.0.1](https://github.com/mohammad-fouladgar/eloquent-builder)

As I explained earlier,you may need to filter a model in your project like `User` model.For example, filtering users based on gender, age, online / offline status and etc... .

Now, you may check if the authenticated user actually has the authority to apply a given filter. For example, you may determine if a user has a premium account or is admin, can apply the `StatusFilter` to get listing the online or offline people.

Let's have a practical example below for a better understanding.

## Setup EloquentBuilder package

In the first, if you have not Laravel,run below command:
```sh 
$ composer create-project --prefer-dist laravel/laravel authorize-filter
```
Now go to root of the your application and install EloquentBuilder:
```sh
$ cd authorize-filter
$ composer require mohammad-fouladgar/eloquent-builder
```
Now, you should create a new directory in the ‍‍`app` directory as `EloquentFilters`.This directory is the storage place of the model filters.

## Create Route and Controller

In this step, we need to create a Route for users listing.So open your `routes/web.php` file and add following route:

```php
Route::get('users', 'UserController@index');
```
Then,you should create a controller named `UserController`:

{% gist https://gist.github.com/mohammad-fouladgar/32a49b4ff9c287511a6128be86be86f9 %}

Now you can open bellow url on your browser:
```sh
http://localhost:8000/users
```
If you running this URL, users listing will be returned.
In the next step, we will implement the `gender` and `status` filters.

## Gender Filter

At this point, we want to filter users based on gender.
First of all, you should create a new directory as `User` inside `EloquentFilters`.This directory contains user model filters.
Next, create a new file named `GenderFilter.php`:

_app/EloquentFilters/User/GenderFilter.php_

{% gist https://gist.github.com/mohammad-fouladgar/2850e3a62d20d92b0c486f3be43d64f1 %}

after creating above filter, open your browser and run this url for apply filter on your query:

```sh
http://localhost:8000/users?filter[gender]=male
```
By running this url,users are listed who have the gender as male.

## StatusFilter

This filter has a `authorize` method.within method we want to check if a user has a premium account, can apply the `StatusFilter` to get listing the online or offline people.

Take a look at the following filter:

_app/EloquentFilters/User/StatusFilter.php_

{% gist https://gist.github.com/mohammad-fouladgar/0744491c081fa1b601d1478b0b66d25a %}

For apply the above filter,in browser open the below url:
```sh
http://localhost:8000/users?filter[gender]=male&filter[status]=online
```
> For see more details click [here](https://github.com/mohammad-fouladgar/eloquent-builder#authorizing-filter) please.

## Conclusion

In this article, we learned how to use the **EloquentBuilder** package in the Laravel framework.

Also learned:

- How to make a filter for a model
- How to apply filters on query builder
- How to check the authorization to apply a given filter

And finally having a clean code, readable and extensible.

I hope this article will be of interest to you. I'm glad to have your valuable comments.

Good luck...