PHP Classes

Laravel Database Filter Result Class: Compose condition filters to retrieve query result

Recommend this page to a friend!
  Info   View files Documentation   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 27 This week: 1All time: 11,088 This week: 560Up
Version License PHP version Categories
laravel-database-fil 1.0Custom (specified...5PHP 5, Databases, Libraries
Description 

Author

This class can compose condition filters to retrieve query result.

It provides a base classes and traits to be create filter implementation classes that define conditions that will be used to compose SQL queries that retrieve database table records that will be mapped to objects.

The filter implementation classes can define one filter condition by specifying field names and values to be matched in the SQL queries.

There may also exist composite filter implementation classes that define a set of conditions which can be defined in separate filter classes.

Picture of Edward Paul
  Performance   Level  
Name: Edward Paul is available for providing paid consulting. Contact Edward Paul .
Classes: 17 packages by
Country: Nigeria Nigeria
Age: 33
All time rank: 273918 in Nigeria Nigeria
Week rank: 33 Up4 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 10x

Winner: 1x

Documentation

Laravel Database Filter

Latest Version on Packagist Build Status Quality Score Total Downloads

Need to filter database results with a query string? Filter Laravel Model With Query Strings

Installation

You can install the package via composer:

composer require infinitypaul/laravel-database-filter

The package will automatically register itself, but if your laravel versions is < 5.5 you will need to add Infinitypaul\LaravelDatabaseFilter\LaravelDatabaseFilterServiceProvider::class, service provider under your config/app.php file.

Usage

Once the package is installed, an artisan command is available to you.

php artisan make:filter 

Generate a new model filter

php artisan make:filter SchoolFilter --model

This package will generate a new PHP file under app/Filters/ folder with the name SchoolFilter.php which will look like below.

<?php

namespace App\Filters;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FiltersAbstract;

class SchoolFilter extends FiltersAbstract {
        protected $filters = [];
}

Add The filterTrait and Register The SchoolFilter On Your Laravel Model Where The Filter Will Be Used.



namespace App;

use App\Filters\SchoolFilter;
use Infinitypaul\LaravelDatabaseFilter\Traits\filterTrait;


class Course extends Model
{
    use filterTrait;

    protected $filter = SchoolFilter::class;

}

Let assume we are to filter our database by checking those who have paid. Then we need to create a new filter class by using the artisan command below.

php artisan make:filter PaidFilter

The above command will also generate a new PHP file under app/Filters folder with the name PaidFilter.php which will look like below.


namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

class PaidFilter extends FilterAbstract
{
    
        public function mappings ()
        {
            return [];
        }

        
        public function filter(Builder $builder, $value)
        {
            return $builder;
        }
}


Then we can register our filter conditions in the SchoolFilter We Created Above.


namespace App\Filters;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FiltersAbstract;

class SchoolFilter extends FiltersAbstract {
        protected $filters = [
            'paid' => PaidFilter::class
        ];
}

The $filters Array Key will be shown in the query string as paid.

    http://localhost:8080/education?paid=free

The value PaidFilter::class takes us to the new class we created with the artisan command.


namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

class PaidFilter extends FilterAbstract
{
    
        public function mappings ()
        {
            return [];
        }

       
        public function filter(Builder $builder, $value)
        {
            return $builder->where('difficulty', $value);
        }
}

The filter method takes in our conditions or whatever checks against the database

To be strict about query params input, we can use the mapping method or leave empty for free entry


namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

class PaidFilter extends FilterAbstract
{
    
        public function mappings ()
        {
            return [
                'free' => true,
                'premium' => false
            ];
        }

With the above setup, You can only accept free or paid in your query params, and their values return accordingly.

Lastly, You should be able to add the filter() passing in your $request in your controller.

namespace App\Http\Controllers;

use App\Course;
use App\Filters\AccessFilter;
use Illuminate\Http\Request;

class CourseController extends Controller
{
    public function index(Request $request){
        return Course::filter($request)->get();
    }
}

You Can Register As Many Filter You Want

<?php

namespace App\Filters;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FiltersAbstract;

class SchoolFilter extends FiltersAbstract {
        protected $filters = [
            'paid' => PaidFilter::class,
            'access' => AccessFilter::class,
            'difficulty' => DifficultyFilter::class
        ];
}

With The Above Settings, Your Url Will End Up Like This

    http://infinitypaul.com/education?paid=free&access=whatever&difficulty=whoever

You can add custom filter to your controller, by passing an array of filter into the filter scope

  public function index(Request $request){
        return Course::filter($request, ['score' => DifficultyFilter::class])->get();
    }

Bug & Features

If you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/infinitypaul/laravel-database-filter/issues.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email infinitypaul@live.com instead of using the issue tracker.

How can I thank you?

Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!

Don't forget to follow me on twitter!

Thanks! Edward Paul.

License

The MIT License (MIT). Please see License File for more information.


  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 4 directories)
Files folder imagetests (1 file)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageAbstracts (2 files)
Files folder imageConsole (1 file)
Files folder imagestubs (2 files)
Files folder imageTraits (2 files)
  Plain text file LaravelDatabaseFilterServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Abstracts  
File Role Description
  Plain text file FilterAbstract.php Class Class source
  Plain text file FiltersAbstract.php Class Class source

  Files folder image Files  /  src  /  Console  
File Role Description
  Plain text file CreateNewFilter.php Class Class source

  Files folder image Files  /  src  /  stubs  
File Role Description
  Plain text file filter.stub Class Class source
  Plain text file model-filter.stub Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file filterTrait.php Class Class source
  Plain text file GenerateFile.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file ExampleTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:27
This week:1
All time:11,088
This week:560Up