Giter VIP home page Giter VIP logo

laravel-filters's Introduction

Laravel Filters

Imagine that ...

Filter with Query String

This URL:

/users?name=myk&age=21&company=rick-and-morty&sort_age=desc

automatically knew to filter the DB query by responding with users that have their

  • name containing myk
  • age as 21
  • company name containing rick-and-morty

and order the records by age in descending order, all without you having to write boilerplate code ๐Ÿ˜ฑ.

Load Relationships

Or that you could automatically include a relationship for a model by adding a ?with_relationship to the URL ๐Ÿ˜, like:

laravel-filters

Hold your horses ๐Ÿ˜œ, I'm about to show you how.

Setup

  • Run composer require mykeels/laravel-filters in your terminal to pull the package in.

Usage

  • In the Model class you wish to make filterable, use the FilterableTrait like:
<?php

use Mykeels\Filters\Traits\FilterableTrait;

class User {
  use FilterableTrait;
  ...
}
  • Create a filter class for that model e.g. UserFilter
<?php
namespace App\Filters;

use App\User;
use Mykeels\Filters\BaseFilters;
use Illuminate\Http\Request;
use Carbon\Carbon;

class UserFilters extends BaseFilters
{
    public function name($term) {
        return $this->builder->where('users.name', 'LIKE', "%$term%");
    }
  
    public function company($term) {
        return $this->builder->whereHas('company', function ($query) use ($term) {
            return $query->where('name', 'LIKE', "%$term%");
        });
    }
  
    public function age($term) {
        $year = Carbon::now()->subYear($age)->format('Y');
        return $this->builder->where('dob', '>=', "$year-01-01")->where('dob', '<=', "$year-12-31");
    }

    public function sort_age($type = null) {
        return $this->builder->orderBy('dob', (!$type || $type == 'asc') ? 'desc' : 'asc');
    }
}

Note how the name of each method maps to the key of each query string in /users?name=myk&age=21&company=rick-and-morty&sort_age=desc, and the parameters of the methods map to the values.

  • Inject UserFilters in your controller ๐Ÿ˜, like:
<?php

namespace App\Http\Controllers;

use App\User;
use App\Filters\UserFilters;

class UserController extends Controller {
  
  public function index(Request $request, UserFilters $filters)
  {
      return User::filter($filters)->get();
  }
}

That's all! ๐Ÿ’ƒ

Now, you can execute your app, and use variations of the query strings that your filters allow for. ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

laravel-filters's People

Contributors

mykeels avatar osule avatar pmller avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.