PHP code example of coderscantina / laravel-filter
1. Go to this page and download the library: Download coderscantina/laravel-filter 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/ */
coderscantina / laravel-filter example snippets
namespace App;
use CodersCantina\Filter\ExtendedFilter;
class TestFilter extends ExtendedFilter
{
public function name($name)
{
return $this->builder->where('name', $name);
}
public function latest()
{
return $this->builder->latest();
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
use CodersCantina\Filter\Filterable;
class TestModel extends Model
{
use Filterable;
}
namespace App\Http\Controllers;
use App\TestModel;
use App\TestFilter;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Collection;
class LessonsController extends Controller
{
/**
* Show all lessons.
*
* @param Request $request
* @return Collection
*/
public function index(Request $request)
{
$filter = new TestFilter($request->all());
// For enhanced security, whitelist allowed filters
$filter->setWhitelistedFilters(['name', 'latest']);
return TestModel::filter($filter)->get();
}
}
namespace App;
use CodersCantina\Filter\AdvancedFilter;
class ProductFilter extends AdvancedFilter
{
public function name($value)
{
// Automatically handles operator syntax
$this->applyDynamicFilter('name', $value);
}
public function price($value)
{
// Supports both operators and range syntax
$this->applyAdvancedRangeFilter('price', $value);
}
public function created_at($value)
{
// Supports both operators and date range syntax
$this->applyAdvancedDateFilter('created_at', $value);
}
}
// Find products with names containing "phone"
$filter = new ProductFilter(['name' => 'like:phone']);
// Find products with price greater than or equal to 100
$filter = new ProductFilter(['price' => 'gte:100']);
// Find products created after January 1, 2023
$filter = new ProductFilter(['created_at' => 'gte:2023-01-01']);
// Find products in specific categories
$filter = new ProductFilter(['category' => 'in:electronics,phones,accessories']);
// Find products that are either active or featured
$filter = new ProductFilter(['status' => ['eq:active', 'eq:featured']]);
// Find non-empty descriptions
$filter = new ProductFilter(['description' => '!empty:']);
// Traditional range syntax still works
$filter = new ProductFilter(['price' => '100...500']);
// Using operators for precise comparisons
$filter = new ProductFilter(['price' => 'gte:100']);
// Traditional date range
$filter = new ProductFilter(['created_at' => '2023-01-01...2023-12-31']);
// Using operators
$filter = new ProductFilter(['created_at' => 'gte:2023-01-01']);
// Products that are either active or pending
$filter = new ProductFilter([
'status' => ['eq:active', 'eq:pending']
]);
$filter = new ProductFilter(['price' => 'between:10,50']);
$filter->setCustomOperators(['between' => 'BETWEEN']);
namespace App;
use CodersCantina\Filter\ExtendedFilter;
use CodersCantina\Filter\AdvancedFilterable;
class CustomFilter extends ExtendedFilter
{
use AdvancedFilterable;
public function status($value)
{
$this->applyDynamicFilter('status', $value);
}
}
public function active($value = true)
{
$this->builder->where('active', $value);
}
public function priceRange($value)
{
$this->applyRangeFilter('price', $value);
}
public function dateCreated($value)
{
$this->applyDateFilter('created_at', $value);
}
protected function isValidColumnName(string $column): bool
{
// Your custom validation logic
return parent::isValidColumnName($column) && in_array($column, $this->allowedColumns);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.