Filtan is a powerful Laravel QueryFilter package designed to simplify and enhance the process of filtering Eloquent queries.
Bot releases are visible (Hide)
β¨ Enhancements
strtolower
method when passing query-string into custom query-function.π§Ό Cleanup
php artisan make:filter
command is executed.π Fixes
php artisan make:filter
command output message.Published by patienceman-universe almost 2 years ago
Sometime you may want to pass the intended function in your query filter, π to not pass through them all!
and well!, it might scale your application to run more faster!, because we have focus! right! π π₯³
Right now, what you need to do is pass those function as parameter into your ->filter(QueryFilter, ['function-as-string'])
function! π₯³ !, Let take quick example:
Let suppose we have Our QueryFilter class as:
namespace App\Filters;
use Patienceman\QueryFilter;
class AirlineFilter extends QueryFilter {
/**
* search given string from airlines by name
* @param string $query
*/
public function query(string $query){
$this->builder->where('name', 'LIKE', '%' . $query . '%');
}
/**
* Get where code
* @param string $code
*/
public function code(string $code){
$this->builder->where("code", "some example code");
}
/**
* Get where location
* @param string $location
*/
public function location(string $location){
$this->builder->where("location", "not attached");
}
}
So to work with that, you will: π remember we want query and code
π₯½ :
/**
* Get all airlines that are open for ticket
*
* @param AirlineFilter $filter
* @return JsonResponse
*/
public function index(AirlineFilter $filter) {
$airlines = Airline::where('ticket', 'active)
->filter($filter, ['query', 'code'])
->get();
return AirlineResource::collection($airlines);
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Published by patienceman-universe almost 2 years ago
Sometime you may want to pass the intended function in your query filter, π to not pass through them all!
and well!, it might scale your application to run more faster!, because we have focus! right! π π₯³
Right now, what you need to do is pass those function as parameter into your ->filter(QueryFilter, ['function-as-string'])
function! π₯³ !, Let take quick example:
Let suppose we have Our QueryFilter class as:
namespace App\Filters;
use Patienceman\QueryFilter;
class AirlineFilter extends QueryFilter {
/**
* search given string from airlines by name
* @param string $query
*/
public function query(string $query){
$this->builder->where('name', 'LIKE', '%' . $query . '%');
}
/**
* Get where code
* @param string $code
*/
public function code(string $code){
$this->builder->where("code", "some example code");
}
/**
* Get where location
* @param string $location
*/
public function location(string $location){
$this->builder->where("location", "not attached");
}
}
So to work with that, you will: π remember we want query and code
π₯½ :
/**
* Get all airlines that are open for ticket
*
* @param AirlineFilter $filter
* @return JsonResponse
*/
public function index(AirlineFilter $filter) {
$airlines = Airline::where('ticket', 'active)
->filter($filter, ['query', 'code'])
->get();
return AirlineResource::collection($airlines);
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Published by patienceman-universe about 2 years ago
We all like automated stuff like
php artisan make:cake BananaCake
that what I was doing for you so you don't have always to create files for filter traditional. π§¨
Just one command π
Let us use our current example of the AirPlane Model and create a new filter:
php artisan make:filter AirPlaneFilter
so it will create the filter file for u, Just in ```bash App\Services\Filters ``
namespace App\Services\Filters;
use Patienceman\Filtan\QueryFilter;
class AirPlaneFilter extends QueryFilter {
/**
* public function query($query) {
* $this->builder->where('name', 'LIKE', '%' . . '%')
* }
*/
}
π₯ π₯ What best move we make: in the world?
So you may want even to specify the custom path for your filter, Just relax and add it in front of your filter name.
Let's take again our current example.
php artisan make:filter Model/AirPlaneFilter
π π That is just what magic can make, awesome right?
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Published by patienceman-universe about 2 years ago
Flltan is fast and reusable laravel package for custom model query filters
To install the package don't require much requirement except to paste the following compand in laravel terminal, and the you're good to go.
composer require patienceman/filtan
In your App/Services directory, create new folrder called Filters, where you gonna put all of your model filter files.
After everything, you can add your custom model filter file, let take example of App/Services/Filters/AirplaneFilters
class.
namespace App\Services\Filters;
use Patienceman\Filtan\QueryFilter;
class AirplaneFilter extends QueryFilter {
public function query(string $query){
$this->builder->where('name', 'LIKE', '%' . $query . '%');
}
}
So now you have your filters function to be applied when new AirplaneModel query called!,
We need to communicate to model and tell that we have it filters, so that we can call it anytime!!,
So let use filterable trait to enable filter builder.
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Patienceman\Filtan\Filterable;
class Airplane extends Model {
use HasFactory, Filterable;
}
Boom Boom, from now on, we are able call our fiter anytime, any place that need Airplane model, so let see how we can use this in our controller
namespace App\Http\Controllers\ApiControllers;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\Filters\CompanyFilter;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AirplaneController extends Controller {
/**
* Display a listing of the resource.
*
* @return JsonResponse
*/
public function index(CompanyFilter $filter): JsonResponse {
$planes = Airplane::allPlanes()->filter($filter)->get();
return successResponse(
AirplaneResource::collection($planes),
AirplaneAlert::DISPLAY_MESSAGE
);
}
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.