1. Go to this page and download the library: Download albetnov/laravel-filterable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
albetnov / laravel-filterable example snippets
namespace App\Models;
use Albet\LaravelFilterable\Enums\FilterableType;
use Albet\LaravelFilterable\Traits\Filterable;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model {
use Filterable;
protected arrray $filterableColumns = [
'ticket_no' => FilterableType::NUMBER,
'customer_name' => FilterableType::TEXT,
'schedule' => FilterableType::DATE
];
protected function filterableColumns(): array {
return [
'customer_address' => FilterableType::custom(),
];
}
}
use Albet\LaravelFilterable\Enums\FilterableType;
use Albet\LaravelFilterable\Enums\Operators;
protected function filterableColumns(): array {
return [
'customer_name' => FilterableType::TEXT->limit([Operators::CONTAINS, Operators::NOT_CONTAINS,
Operators::STARTS_WITH, Operators::ENDS_WITH])
];
}
use Albet\LaravelFilterable\Enums\FilterableType;
use Illuminate\Database\Eloquent\Relations\HasOne;
protected function filterableColumns(): array {
return [
'flight_license' => FilterableType::NUMBER->related('flight', fn($query) => $query->where('status', 'A'))
];
}
public function flight(): HasOne {
$this->hasOne(Flight::class);
}
use Albet\LaravelFilterable\Enums\FilterableType;
FilterableType::custom();
use Albet\LaravelFilterable\Enums\FilterableType;
use Albet\LaravelFilterable\Enums\Operators;
use Albet\LaravelFilterable\Operator;
use Albet\LaravelFilterable\Traits\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model {
use Filterable;
public function filterableColumns(): array{
return [
'customer_address' => FilterableType::custom([Operators::CONTAINS, Operators::NOT_CONTAINS])
];
}
public function filterCustomAddress(Builder $builder, string $operator, string $value): void {
dump($operator); // 'contains' or 'not_contains' (raw string operator)
dump($value); // raw string value
$builder->whereHas('customer', fn($query) => $query->where('name', 'LIKE', "%$value%"));
}
}
use App\Models\Flight;
// In this example I choose to merge the request, alternatively you can hit endpoint like this:
// http://localhost:8000/all-flights?filters[0][field]=customer_name&filters[0][operator]=eq&filters[0][value]=asep
request()->merge([
'filters' => [
[
'operator' => 'eq',
'field' => 'customer_name',
'value' => 'asep'
]
]
]);
dd(Flight::filter()->get()); // Flight[{customer_name: "asep", ticket_no: 20393, schedule: "2023-08-20"}]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.