-
Notifications
You must be signed in to change notification settings - Fork 121
Configuration
composer require tucker-eric/eloquentfilter
There are a few ways to define the filter a model will use:
- Use EloquentFilter's Default Settings
- Use A Custom Namespace For All Filters
- Define A Model's Default Filter
- Dynamically Select A Model's Filter
The default namespace for all filters is App\ModelFilters\
and each Model expects the filter classname to follow the {$ModelName}Filter
naming convention regardless of the namespace the model is in. Here is an example of Models and their respective filters based on the default naming convention.
Model | ModelFilter |
---|---|
App\User |
App\ModelFilters\UserFilter |
App\FrontEnd\PrivatePost |
App\ModelFilters\PrivatePostFilter |
App\FrontEnd\Public\GuestPost |
App\ModelFilters\GuestPostFilter |
Registering the service provider will give you access to the
php artisan model:filter {model}
command as well as allow you to publish the configuration file. Registering the service provider is not required and only needed if you want to change the default namespace or use the artisan command
After installing the Eloquent Filter library, register the EloquentFilter\ServiceProvider::class
in your config/app.php
configuration file:
'providers' => [
// Other service providers...
EloquentFilter\ServiceProvider::class,
],
Copy the package config to your local config with the publish command:
php artisan vendor:publish --provider="EloquentFilter\ServiceProvider"
In the config/eloquentfilter.php
config file. Set the namespace your model filters will reside in:
'namespace' => "App\\ModelFilters\\",
Create a public method modelFilter()
that returns $this->provideFilter(Your\Model\Filter::class);
in your model.
<?php namespace App;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Filterable;
public function modelFilter()
{
return $this->provideFilter(App\ModelFilters\CustomFilters\CustomUserFilter::class);
}
//User Class
}
You can define the filter dynamically by passing the filter to use as the second parameter of the filter()
method. Defining a filter dynamically will take precedent over any other filters defined for the model.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\ModelFilters\Admin\UserFilter as AdminFilter;
use App\ModelFilters\User\UserFilter as BasicUserFilter;
use Auth;
class UserController extends Controller
{
public function index(Request $request)
{
$userFilter = Auth::user()->isAdmin() ? AdminFilter::class : BasicUserFilter::class;
return User::filter($request->all(), $userFilter)->get();
}
}
Only available if you have registered
EloquentFilter\ServiceProvider::class
in the providers array in your `config/app.php'
You can create a model filter with the following artisan command:
php artisan model:filter User
Where User
is the Eloquent Model you are creating the filter for. This will create app/ModelFilters/UserFilter.php
The command also supports psr-4 namespacing for creating filters. You just need to make sure you escape the backslashes in the class name. For example:
php artisan model:filter AdminFilters\\User
This would create app/ModelFilters/AdminFilters/UserFilter.php