### Dynamically Whitelist a Method Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Example of dynamically whitelisting a method within the setup() method, allowing a blacklisted method to be called under certain conditions. ```php public function setup() { if(Auth::user()->isAdmin()) { $this->whitelistMethod('secretMethod'); } } ``` -------------------------------- ### Define User Filter Methods Source: https://github.com/tucker-eric/eloquentfilter/wiki/Usage This example shows how to define filter methods for specific input keys like 'company_id', 'name', and 'mobile_phone'. It also demonstrates the use of the 'setup' method for initial query modifications and a 'secretMethod' that is blacklisted. ```php class UserFilter extends ModelFilter { protected $blacklist = ['secretMethod']; // This will filter 'company_id' OR 'company' public function company($id) { return $this->where('company_id', $id); } public function name($name) { return $this->where(function($q) use ($name) { return $q->where('first_name', 'LIKE', "%{$name}%") ->orWhere('last_name', 'LIKE', "%{$name}%"); }); } public function mobilePhone($phone) { return $this->where('mobile_phone', 'LIKE', "{$phone}%"); } public function setup() { $this->onlyShowDeletedForAdmins(); } public function onlyShowDeletedForAdmins() { if(Auth::user()->isAdmin()) { $this->withTrashed(); } } public function secretMethod($secretParameter) { return $this->where('some_column', true); } } ``` -------------------------------- ### Define User Filter Logic Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Example of defining filter methods for 'company_id', 'name', and 'mobile_phone' inputs. Includes a setup method and a blacklisted method. ```php use EloquentFilter\ModelFilter; class UserFilter extends ModelFilter { protected $blacklist = ['secretMethod']; // This will filter 'company_id' OR 'company' public function company($id) { return $this->where('company_id', $id); } public function name($name) { return $this->where(function($q) use ($name) { return $q->where('first_name', 'LIKE', "% $name %") ->orWhere('last_name', 'LIKE', "% $name %"); }); } public function mobilePhone($phone) { return $this->where('mobile_phone', 'LIKE', "$phone%"); } public function setup() { $this->onlyShowDeletedForAdmins(); } public function onlyShowDeletedForAdmins() { if(Auth::user()->isAdmin()) { $this->withTrashed(); } } public function secretMethod($secretParameter) { return $this->where('some_column', true); } } ``` -------------------------------- ### Example Request Parameters Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md This shows the raw input received from a request with various filter parameters. ```php [ 'name' => 'er', 'last_name' => '', 'company_id' => '2', 'roles' => ['1','4','7'], 'industry' => '5' ] ``` -------------------------------- ### Install EloquentFilter via Composer Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Use Composer to install the EloquentFilter package. ```bash composer require tucker-eric/eloquentfilter ``` -------------------------------- ### Input for filtering users by client attributes Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Example input array used to filter User models based on their related Client's industry and potential volume. ```php $input = [ 'industry' => '5', 'potential_volume' => '10000' ]; ``` -------------------------------- ### Example of Aliased Input Transformation Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Demonstrates how aliased input keys in the '$relations' array are transformed to match the filter method names on the related model's filter. ```php [ 'client_industry' => 1, 'client_potential' => 100000 ] ``` ```php [ 'industry' => 1, 'potential_volume' => 100000 ] ``` -------------------------------- ### UserFilter setup for clients relationship Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Applies a default 'hasRevenue' scope to all queries on the 'clients' relationship within the UserFilter. ```php class UserFilter extends ModelFilter { public function clientsSetup($query) { return $query->hasRevenue(); } } ``` -------------------------------- ### Dynamically Whitelisting a Method Source: https://github.com/tucker-eric/eloquentfilter/wiki/Blacklist This snippet demonstrates how to dynamically whitelist a method ('secretMethod') within the setup() method if a specific condition (Auth::user()->isAdmin()) is met. The whitelisted method can then be invoked by the filter based on input. ```php protected $blacklist = ['secretMethod']; public function setup() { if(Auth::user()->isAdmin()) { $this->whitelistMethod('secretMethod'); } } public function secretMethod($val) { return $this->where('some_column', $val); } ``` -------------------------------- ### Apply Filter to Model Query in Controller Source: https://github.com/tucker-eric/eloquentfilter/wiki/Usage This example demonstrates how to apply the defined filters to a User model query within a controller using the filter() method and passing request data. ```php class UserController extends Controller { public function index(Request $request) { return User::filter($request->all())->get(); } } ``` -------------------------------- ### Eloquent Filter Usage Source: https://github.com/tucker-eric/eloquentfilter/wiki/Home This example shows how to use Eloquent Filter to achieve the same filtering results as the traditional method, but with significantly less code. It applies filters directly from the request array. ```php all())->get(); } } ``` -------------------------------- ### Traditional Eloquent Filtering Source: https://github.com/tucker-eric/eloquentfilter/wiki/Home This example demonstrates the traditional, verbose method of filtering Eloquent models based on multiple request parameters, including handling optional parameters and relationships. ```php input('company_id')); if ($request->has('last_name')) { $query->where('last_name', 'LIKE', '%' . $request->input('last_name') . '%'); } if ($request->has('name')) { $query->where(function ($q) use ($request) { return $q->where('first_name', 'LIKE', $request->input('name') . '%') ->orWhere('last_name', 'LIKE', '%' . $request->input('name') . '%'); }); } $query->whereHas('roles', function ($q) use ($request) { return $q->whereIn('id', $request->input('roles')); }) ->whereHas('clients', function ($q) use ($request) { return $q->whereHas('industry_id', $request->input('industry')); }); return $query->get(); } } ``` -------------------------------- ### UserFilter with clientsSetup Method Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Applies a 'hasRevenue' scope to the clients relationship whenever the UserFilter is applied to the clients relationship. This ensures only users with clients that have revenue are considered. ```php class UserFilter extends ModelFilter { public function clientsSetup($query) { return $query->hasRevenue(); } } ``` -------------------------------- ### Filtering with Eloquent Filter Package Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Shows how to use the Eloquent Filter package to achieve the same filtering results with a significantly more concise syntax. ```php all())->get(); } } ``` -------------------------------- ### Publish EloquentFilter Configuration Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Publish the EloquentFilter configuration file to your local config directory using the vendor:publish artisan command. ```bash php artisan vendor:publish --provider="EloquentFilter\ServiceProvider" ``` -------------------------------- ### Basic Pagination with Query String Preservation Source: https://github.com/tucker-eric/eloquentfilter/wiki/Pagination Use `paginateFilter()` to paginate query results. It automatically appends the original query string, ignoring empty inputs, to the pagination links. ```php {!! $pages->appends(Input::except('page'))->render() !!} ``` -------------------------------- ### Combined Filtering Strategies Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Demonstrates how to combine direct filtering with relationship-based filtering. This approach ensures related models are queried only once. ```php ['industry'], ]; public function clientsSetup($query) { return $query->hasRevenue(); } public function name($name) { return $this->where(function($q) { return $q->where('first_name', 'LIKE', $name . '%')->orWhere('last_name', 'LIKE', '%' . $name.'%'); }); } public function potentialVolume($volume) { return $this->related('clients', 'potential_volume', '>=', $volume); } public function lastName($lastName) { return $this->where('last_name', 'LIKE', '%' . $lastName); } public function company($id) { return $this->where('company_id',$id); } public function roles($ids) { return $this->whereHas('roles', function($query) use ($ids) { return $query->whereIn('id', $ids); }); } } ``` -------------------------------- ### User Model with hasMany Client relationship Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Defines the User model and its relationship to Client models. ```php class User extends Model { use Filterable; public function clients() { return $this->hasMany(Client::class); } } ``` -------------------------------- ### Configure Model Filter Namespace Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Set the namespace for your model filters in the config/eloquentfilter.php configuration file. ```php 'namespace' => "App\\ModelFilters\\", ``` -------------------------------- ### Client Model with belongsTo Industry relationship Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Defines the Client model, its relationship to Industry, and a local scope for filtering by revenue. ```php class Client extends Model { use Filterable; public function industry() { return $this->belongsTo(Industry::class); } public function scopeHasRevenue($query) { return $query->where('total_revenue', '>', 0); } } ``` -------------------------------- ### Filtering related clients by industry ID using related() with a closure Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Demonstrates using a closure with the `related()` method to apply custom filtering logic to the 'clients' relationship. ```php $this->related('clients', function($query) use ($id) { return $query->where('industry_id', $id); }); ``` -------------------------------- ### Implementing SimplePaginateFilter in a Controller Source: https://github.com/tucker-eric/eloquentfilter/wiki/Pagination Use `simplePaginateFilter()` for a simpler pagination view (previous/next links only). It also preserves the query string similar to `paginateFilter()`. ```php public function simpleIndex(Request $request) { $users = User::filter($request->all())->simplePaginateFilter(); return view('users.index', compact('users')); } } ``` -------------------------------- ### Generate Model Filter Artisan Command Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Use the artisan command to generate a new model filter class. ```bash php artisan model:filter User ``` -------------------------------- ### Paginate Query Results Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Utilize paginateFilter() and simplePaginateFilter() to paginate query results while preserving the URL query string. These methods accept the same input as Laravel's paginator. ```php public function index(Request $request) { $users = User::filter($request->all())->paginateFilter(); return view('users.index', compact('users')); } ``` ```php public function simpleIndex(Request $request) { $users = User::filter($request->all())->simplePaginateFilter(); return view('users.index', compact('users')); } ``` -------------------------------- ### Dynamically Push Values to Relation Filters Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Use the `push()` method to pass data to a relation's filter method based on conditional logic within another filter. This avoids redundant queries. ```php public $relations = [ 'clients' => ['industry', 'status'], ]; public function statusType($type) { if($type === 'all') { $this->push('status', 'all'); } } ``` -------------------------------- ### Register EloquentFilter Service Provider Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Register the EloquentFilter Service Provider in your config/app.php file to enable its features. ```php 'providers' => [ // Other service providers... EloquentFilter\ServiceProvider::class, ], ``` -------------------------------- ### Filter Related Models With Both Methods Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Demonstrates how to filter related models using both direct and relation-based methods, ensuring the related model is queried only once. This is useful for complex filtering scenarios involving joined tables. ```php ['industry'], ]; public function clientsSetup($query) { return $query->hasRevenue(); } public function name($name) { return $this->where(function($q) { return $q->where('first_name', 'LIKE', $name . '%')->orWhere('last_name', 'LIKE', '%' . $name.'%'); }); } public function potentialVolume($volume) { return $this->related('clients', 'potential_volume', '>=', $volume); } public function lastName($lastName) { return $this->where('last_name', 'LIKE', '%' . $lastName); } public function company($id) { return $this->where('company_id',$id); } public function roles($ids) { return $this->whereHas('roles', function($query) use ($ids) { return $query->whereIn('id', $ids); }); } } ``` -------------------------------- ### Generate Namespaced Model Filter Artisan Command Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Generate a model filter class within a specific namespace using the artisan command. ```bash php artisan model:filter AdminFilters\\User ``` -------------------------------- ### UserFilter using related() with a Closure Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Demonstrates filtering users by client industry using the related() method with a closure. The closure injects the related model's query builder for custom filtering. ```php $this->related('clients', function($query) use ($id) { return $query->where('industry_id', $id); }); ``` -------------------------------- ### Implementing PaginateFilter in a Controller Source: https://github.com/tucker-eric/eloquentfilter/wiki/Pagination In your controller, apply the `paginateFilter()` method after your Eloquent query and filter. This method accepts the same inputs as Laravel's basic paginator. ```php class UserController extends Controller { public function index(Request $request) { $users = User::filter($request->all())->paginateFilter(); return view('users.index', compact('users')); } } ``` -------------------------------- ### Dynamically Select Model Filter Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Dynamically select a filter for a model by passing the filter class as the second argument to the filter() method. This takes precedence over any other filter definitions. ```php isAdmin() ? AdminFilter::class : BasicUserFilter::class; return User::filter($request->all(), $userFilter)->get(); } } ``` -------------------------------- ### Register EloquentFilter Service Provider in Lumen Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Register the EloquentFilter Lumen Service Provider in your Lumen application's bootstrap/app.php file. ```php $app->register(EloquentFilter\LumenServiceProvider::class); ``` -------------------------------- ### Implement Relation Filtering Methods Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Implement methods in the related model's filter class that correspond to the input keys defined in the '$relations' array. These methods will contain the actual filtering logic. ```php class ClientFilter extends ModelFilter { public $relations = []; public function industry($id) { return $this->where('industry_id', $id); } public function potentialVolume($volume) { return $this->where('potential_volume', '>=', $volume); } } ``` -------------------------------- ### Client Model with belongsTo and scopeHasRevenue Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Defines a Client model with a 'industry' relationship and a 'hasRevenue' scope. ```php class Client extends Model { use Filterable; public function industry() { return $this->belongsTo(Industry::class); } public function scopeHasRevenue($query) { return $query->where('total_revenue', '>', 0); } } ``` -------------------------------- ### EloquentFilter Helper Methods Source: https://github.com/tucker-eric/eloquentfilter/wiki/Usage These are helper methods provided by the Filterable trait that can be used directly within your EloquentFilter classes to perform common query operations like LIKE, begins with, and ends with. ```php $this->whereLike($column, $string) ``` ```php $this->whereBeginsWith($column, $string) ``` ```php $this->whereEndsWith($column, $string) ``` -------------------------------- ### Alias Relation Input Keys Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Use aliasing in the '$relations' array to map descriptive input keys to the actual filter method names on the related model's filter. This allows for more flexible input naming. ```php class UserFilter extends ModelFilter { public $relations = [ 'clients' => [ 'client_industry' => 'industry', 'client_potential' => 'potential_volume' ] ]; } ``` -------------------------------- ### Filtering related clients by industry ID using related() Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Filters User models by their clients' industry ID using the `related()` method in the UserFilter. ```php class UserFilter extends ModelFilter { public function industry($id) { return $this->related('clients', 'industry_id', '=', $id); // This would also be shorthand for the same query // return $this->related('clients', 'industry_id', $id); } public function potentialVolume($volume) { return $this->related('clients', 'potential_volume', '>=', $volume); } } ``` -------------------------------- ### Generate Model Filter Artisan Command Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Use the artisan command 'model:filter' to generate a new model filter class. The command supports PSR-4 namespacing. ```bash php artisan model:filter User ``` ```bash php artisan model:filter AdminFilters\\User ``` -------------------------------- ### UserFilter using related() for Industry Filtering Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Filters users by client industry using the related() method. This method is suitable when the related model doesn't have its own filter or for defining local filtering logic. ```php class UserFilter extends ModelFilter { public function industry($id) { return $this->related('clients', 'industry_id', '=', $id); // This would also be shorthand for the same query // return $this->related('clients', 'industry_id', $id); } public function potentialVolume($volume) { return $this->related('clients', 'potential_volume', '>=', $volume); } } ``` -------------------------------- ### Dynamically Select Model Filter Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Dynamically select a filter for a model based on conditions within the controller. ```php isAdmin() ? AdminFilter::class : BasicUserFilter::class; return User::filter($request->all(), $userFilter)->get(); } } ``` -------------------------------- ### Define Relations for Filtering Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Define the relations and their associated input keys in the `$relations` array within your ModelFilter. This allows querying multiple columns on a relation's table. ```php class UserFilter extends ModelFilter { public $relations = [ 'clients' => ['industry', 'potential_volume'], ]; } ``` -------------------------------- ### Filter Methods for Related Models Source: https://github.com/tucker-eric/eloquentfilter/wiki/Filtering-By-Relationships Implement filter methods for related models. These methods will be called based on the keys defined in the `$relations` array of the parent model's filter. ```php class ClientFilter extends ModelFilter { public $relations = []; public function industry($id) { return $this->where('industry_id', $id); } public function potentialVolume($volume) { return $this->where('potential_volume', '>=', $volume); } } ``` -------------------------------- ### Change EloquentFilter Default Namespace in Lumen Source: https://github.com/tucker-eric/eloquentfilter/blob/master/README.md Modify the default namespace for model filters in Lumen's bootstrap/app.php file. ```php config(['eloquentfilter.namespace' => "App\\Models\\ModelFilters\\"]); ``` -------------------------------- ### Define Default Model Filter Source: https://github.com/tucker-eric/eloquentfilter/wiki/Configuration Define a modelFilter method in your Eloquent model to specify its default filter class. ```php provideFilter(App\ModelFilters\CustomFilters\CustomUserFilter::class); } //User Class } ``` -------------------------------- ### Implement Filterable Trait on Eloquent Model Source: https://github.com/tucker-eric/eloquentfilter/wiki/Usage This snippet shows how to use the EloquentFilter\Filterable trait within an Eloquent model class to enable the filter functionality. ```php