Download .zip |
Info | Documentation | View files (19) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-04-02 (1 month ago) | Not yet rated by the users | Total: 23 | All time: 9,890 This week: 329 |
Version | License | PHP version | Categories | |||
laravel-database-fil 1.0 | Custom (specified... | 5 | PHP 5, Databases, Libraries |
Description | Author | |
This class can compose condition filters to retrieve query result. |
|
Need to filter database results with a query string? Filter Laravel Model With Query Strings
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.
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();
}
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.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email infinitypaul@live.com instead of using the issue tracker.
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.
The MIT License (MIT). Please see License File for more information.
Files |
File | Role | Description | ||
---|---|---|---|---|
src (1 file, 4 directories) | ||||
tests (1 file) | ||||
.editorconfig | Data | Auxiliary data | ||
.scrutinizer.yml | Data | Auxiliary data | ||
.styleci.yml | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
CONTRIBUTING.md | Data | Auxiliary data | ||
LICENSE.md | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description | ||
---|---|---|---|---|
Abstracts (2 files) | ||||
Console (1 file) | ||||
stubs (2 files) | ||||
Traits (2 files) | ||||
LaravelDatabaseFilterServiceProvider.php | Class | Class source |
Files | / | src | / | Abstracts |
File | Role | Description |
---|---|---|
FilterAbstract.php | Class | Class source |
FiltersAbstract.php | Class | Class source |
Files | / | src | / | stubs |
File | Role | Description |
---|---|---|
filter.stub | Class | Class source |
model-filter.stub | Class | Class source |
Files | / | src | / | Traits |
File | Role | Description |
---|---|---|
filterTrait.php | Class | Class source |
GenerateFile.php | Class | Class source |
laravel-database-fil-2020-04-02.zip 12KB | |
laravel-database-fil-2020-04-02.tar.gz 8KB | |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.