PHP code example of mhomayoun / laravel7-filterable
1. Go to this page and download the library: Download mhomayoun/laravel7-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/ */
mhomayoun / laravel7-filterable example snippets
public function recent($minutes = null): \Illuminate\Database\Eloquent\Builder
{
$minutes = (is_numeric($minutes)) ? $minutes : 30;
return $this->builder->where('created_at', '>=', Carbon\Carbon::now()->subMinutes($minutes));
}
return [
'namespace' => 'Http\Filters',
...
];
namespace App\Filters;
use Kyslik\LaravelFilterable\Filter;
class UserFilter extends Filter
{
public function filterMap(): array
{
return ['recent' => ['recently', 'recent']];
}
public function recent($minutes = null)
{
$minutes = (is_numeric($minutes)) ? $minutes : 30;
return $this->builder->where('created_at', '>=', \Carbon\Carbon::now()->subMinutes($minutes)->toDateTimeString());
}
}
use Kyslik\LaravelFilterable\Filterable;
...
class User extends Model
{
use Filterable;
...
}
use App\Filters\UserFilter;
...
public function index(User $user, UserFilter $filters)
{
return $user->filter($filters)->paginate();
}
namespace App\Filters;
use Kyslik\LaravelFilterable\Generic\Filter;
class UserFilter extends Filter
{
protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
}
use Kyslik\LaravelFilterable\Filterable;
...
class User extends Model
{
use Filterable;
...
}
use App\Filters\UserFilter;
...
public function index(User $user, UserFilter $filters)
{
return $user->filter($filters)->paginate();
}
use Kyslik\LaravelFilterable\Generic\Filter
...
class UserFilter extends Filter
{
protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
protected function settings()
{
// global settings for this filter, pick either "except" or "only" logic
$this->only(['=', '~', '!~']);
// $this->except(['!=']);
// settings applied only to some columns, these settings ignore the "global" settings above
$this->for(['username', 'id'])->only(['!=', '>=', '=', '~']);
$this->for(['id'])->only(['=', '!=', '~']); // settings for "id" will be re-written
}
}
public function index(User $user, UserFilter $filter)
{
// will redirect and "apply" the `recent` and `filter-id` filters
// if not a single filter from UserFilter is applied
$filter->default(['recent' => now()->toDateTimeString(), 'filter-id' => '!=5']);
return $user->filter($filter)->paginate();
}