### Publishing Laravel Purity Configuration Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/guide/installation.md This command publishes the default configuration file for Laravel Purity to your application's `config` directory. The `purity.php` file allows you to customize various aspects of the package's behavior and settings. ```sh php artisan vendor:publish --tag=purity ``` -------------------------------- ### Installing Laravel Purity via Composer Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/guide/installation.md This command installs the Laravel Purity package into your Laravel project using Composer, adding it as a required dependency. It fetches the package from Packagist and integrates it into your application's vendor directory. ```sh composer require abbasudo/laravel-purity ``` -------------------------------- ### Complex Filtering with OR and AND in JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This example showcases complex filtering by combining multiple conditions using `$or` and `$and` operators. It constructs a query with `qs` to find books published on '2020-01-01' OR '2020-01-02' AND authored by 'Kai doe', resulting in a GET request like `/api/books?filters[$or][0][date][$eq]=2020-01-01&filters[$or][1][date][$eq]=2020-01-02&filters[author][name][$eq]=Kai%20doe`. ```javascript const qs = require('qs'); const query = qs.stringify({ filters: { $or: [ { date: { $eq: '2020-01-01', }, }, { date: { $eq: '2020-01-02', }, }, ], author: { name: { $eq: 'Kai doe', }, }, }, }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/books?${query}`); ``` -------------------------------- ### Filtering Data with In Filter in JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This example illustrates how to use the 'in' (`$in`) operator to find multiple records based on a list of IDs. It utilizes the `qs` library to build a query for finding restaurants with IDs 3, 6, or 8, resulting in a GET request like `/api/restaurants?filters[id][$in][0]=3&filters[id][$in][1]=6&filters[id][$in][2]=8`. ```javascript const qs = require('qs'); const query = qs.stringify({ filters: { id: { $in: [3, 6, 8], }, }, }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/restaurants?${query}`); ``` -------------------------------- ### Complex Relation Filtering in JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This example illustrates combining multiple relation filters for more precise data retrieval. It uses the `qs` library to construct a query that finds restaurants owned by a chef whose restaurants have a 5-star rating AND a specific cuisine ('Italian'), resulting in a GET request like `/api/restaurants?filters[chef][restaurants][stars][$eq]=5&filters[chef][restaurants][cuisine][$eq]=Italian`. ```javascript const qs = require('qs'); const query = qs.stringify({ filters: { chef: { restaurants: { stars: { $eq: 5, }, cuisine: { $eq: 'Italian', }, }, }, }, }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/restaurants?${query}`); ``` -------------------------------- ### Applying Basic Sorting with Multiple Fields and Order - JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/sort.md This example illustrates how to construct a query string for sorting on multiple fields ('title', 'slug') while explicitly defining the sort order (ascending for 'title', descending for 'slug'). The 'qs' library is used to format the query for a Laravel Purity API request. ```JavaScript const qs = require('qs'); const query = qs.stringify({ sort: ['title:asc', 'slug:desc'], }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/articles?${query}`); ``` -------------------------------- ### Constructing Filtered API Queries - HTTP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/available-methods.md This snippet illustrates the basic syntax for constructing API queries with filters using Laravel Purity. It shows how to specify a field, an operator, and a value within the 'filters' parameter in a GET request. This is crucial for front-end developers interacting with a Laravel Purity-powered API. ```HTTP GET /api/posts?filters[field][operator]=value ``` -------------------------------- ### Filtering Posts with Directly Provided Parameters in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/param.md This example illustrates how to override Purity's default parameter source by directly passing an array of filter conditions to the `filter` method. It includes examples of simple equality filtering and complex `$or` conditions with nested properties for advanced queries. ```php Post::filter($params)->get(); Post::filter([ 'title' => ['$eq' => 'good post'] ])->get(); Post::filter([ '$or' => [ 'title' => [ '$eq' => 'good post' ], 'author' => [ 'name' => [ '$eq' => 'John Doe' ], 'age' => [ '$gt' => 20 ], ], ], ])->get(); ``` -------------------------------- ### Basic Filtering and Sorting with Laravel Purity Source: https://github.com/abbasudo/laravel-purity/blob/master/README.md This example demonstrates the fundamental usage of Laravel Purity. It shows how to enable filtering on an Eloquent query by calling the `filter()` method on a model, and then illustrates how to apply a specific filter (e.g., title containing 'Purity') using a URL query string parameter. This allows frontend users to dynamically filter data. ```php $posts = Post::filter()->get(); ``` ```http GET /api/posts?filters[title][$contains]=Purity ``` -------------------------------- ### Defining Filters in Livewire Component (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/livewire.md This PHP snippet demonstrates how to define a `$filters` public property in a Livewire component, annotated with `#[Url]` for URL synchronization. It then shows how to apply these filters to a `Transaction` model using `Transaction::filter($this->filters)->get()` within the `render` method, preparing data for the view. ```php // component #[Url] public $filters = [ 'title' => [], ]; public function render() { $transactions = Transaction::filter($this->filters)->get(); return view('livewire.transaction-table',compact('transactions')); } ``` -------------------------------- ### Filtering Posts via URL Query String - HTTP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/introduction.md This example illustrates how frontend users can apply a filter to the `/api/posts` endpoint using URL query parameters. It filters posts where the `title` field contains the string 'Purity', showcasing the `filters[field][$operator]=value` syntax used by Laravel Purity. ```HTTP GET /api/posts?filters[title][$contains]=Purity ``` -------------------------------- ### Complex Relation Filtering in Laravel PHP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This Laravel PHP example demonstrates how to apply complex relation filters directly on the server-side. It shows how to pass an array of nested filter parameters to the `filter()` method of a `Restaurant` model to retrieve restaurants based on chef's restaurant stars and cuisine. ```php $params = [ 'filters' => [ 'chef' => [ 'restaurants' => [ 'stars' => [ '$eq' => 5, ], 'cuisine' => [ '$eq' => 'Italian', ], ], ], ], ]; $restaurants = Restaurant::filter($params)->get(); ``` -------------------------------- ### Filtering Data with Between Filter in JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This snippet demonstrates filtering data within a specified range using the 'between' (`$between`) operator. It uses the `qs` library to construct a query that finds users whose 'age' is between 20 and 30, resulting in a GET request like `/api/users?filters[age][$between][0]=20&filters[age][$between][1]=30`. ```javascript const qs = require('qs'); const query = qs.stringify({ filters: { age: { $between: [20, 30], }, }, }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/users?${query}`); ``` -------------------------------- ### Filtering Posts with Default Query Params in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/param.md This snippet demonstrates how Purity's `filter` method automatically extracts filter parameters from the `find` index in the request query string. It shows a typical GET request URL structure for filtering posts by name, illustrating the default behavior. ```php // this will get filters from find query params // `GET /api/users?find[name][$eq]=John` Post::filter(request()->query('find'))->get(); ``` -------------------------------- ### Relation Filtering in JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This snippet demonstrates how to filter data based on fields within a related model. It uses the `qs` library to build a query that finds restaurants owned by a chef whose associated restaurants have a 5-star rating, resulting in a GET request like `/api/restaurants?filters[chef][restaurants][stars][$eq]=5`. ```javascript const qs = require('qs'); const query = qs.stringify({ filters: { chef: { restaurants: { stars: { $eq: 5, }, }, }, }, }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/restaurants?${query}`); ``` -------------------------------- ### Filtering Data with Eq Filter in JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/filter.md This snippet demonstrates how to filter data using the equality (`$eq`) operator in JavaScript with Laravel Purity. It shows how to use the `qs` library to construct a query that finds users where the 'username' field is 'John', resulting in a GET request like `/api/users?filters[name][$eq]=John`. ```javascript const qs = require('qs'); const query = qs.stringify({ filters: { username: { $eq: 'John', }, }, }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/users?${query}`); ``` -------------------------------- ### Restricting Filters by Field using $restrictedFilters (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This snippet demonstrates the second method for field-level filter restriction using the `$restrictedFilters` property. It allows specifying which operators are restricted for certain fields, providing an alternative to `$filterFields`. For example, 'title' can be limited to '$eq' and '$in' operators. ```PHP $restrictedFields = [ 'title' => ['$eq', '$eq'], // title will be limited to the eq operator 'title:$eq,$in' // same as above 'title' // this won't be restricted to any operator ]; ``` -------------------------------- ### Applying Basic Sorting with Multiple Fields - JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/sort.md This snippet demonstrates how to construct a query string for sorting on multiple fields (e.g., 'title' and 'slug') in ascending order. It uses the 'qs' library to properly stringify the sort array for a Laravel Purity API request. ```JavaScript const qs = require('qs'); const query = qs.stringify({ sort: ['title', 'slug'], }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/articles?${query}`); ``` -------------------------------- ### Sorting Posts with Directly Provided Parameters in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/param.md This snippet demonstrates how to apply sorting to posts by directly providing an array of sort fields to Purity's `sort` method. It shows how to specify both ascending (default) and descending order for multiple fields, offering explicit control over the sorting criteria. ```php Post::sort([ 'title', 'id:desc' ])->get(); ``` -------------------------------- ### Applying Purity Filter to Eloquent Query - PHP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/introduction.md This snippet demonstrates the basic usage of Laravel Purity by applying the `filter()` method directly to an Eloquent query. This enables the model to process filtering and sorting parameters from the URL query string, simplifying data retrieval logic for frontend applications. ```PHP $posts = Post::filter()->get(); ``` -------------------------------- ### Applying Sorting by Relationship Field - JavaScript Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/js-examples/sort.md This snippet shows how to construct a query string to sort records based on a related model's field (e.g., 'tags.name'). It demonstrates using dot notation within the sort parameter and the 'qs' library to prepare the request for a Laravel Purity API. ```JavaScript const qs = require('qs'); const query = qs.stringify({ sort: ['tags.name:asc'], }, { encodeValuesOnly: true, // prettify URL }); await request(`/api/posts?${query}`); ``` -------------------------------- ### Applying Purity Sorting in Laravel Controller (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/guide/basic-usage.md This code demonstrates how to apply Purity's sorting capabilities within a Laravel controller. By invoking the `sort()` method on the `Post` model's Eloquent query, the application can process incoming sort parameters from the request and apply them to the database query before fetching the results. ```php use App\Models\Post; class PostController extends Controller { public function index() { return Post::sort()->get(); } } ``` -------------------------------- ### Applying Purity Filters in Laravel Controller (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/guide/basic-usage.md This code illustrates how to apply Purity's filtering capabilities within a Laravel controller. By calling the `filter()` method on the `Post` model's Eloquent query, the application can process incoming filter parameters from the request and apply them to the database query before retrieving the results. ```php use App\Models\Post; class PostController extends Controller { public function index() { return Post::filter()->get(); } } ``` -------------------------------- ### Binding Filters in Livewire Blade Template (HTML) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/livewire.md This HTML snippet illustrates how to bind the `$filters` variable defined in the Livewire component to an input field within a Blade template. Using `wire:model.live="filters.title.$eq"`, it enables real-time filtering based on the input value for the 'title' field, specifically for an exact match (`$eq`). ```html ``` -------------------------------- ### Adding Filterable Trait to Laravel Model (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/guide/basic-usage.md This snippet demonstrates how to integrate the `Filterable` trait from `Abbasudo\Purity` into a Laravel Eloquent model. By using this trait, the model gains access to Purity's filtering functionalities, allowing for dynamic data filtering based on incoming requests. ```php use Abbasudo\Purity\Traits\Filterable; class Post extends Model { use Filterable; // } ``` -------------------------------- ### Disabling Silent Exceptions in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/silent.md This snippet shows how to configure the Purity library to throw exceptions instead of silencing them. By setting the 'silent' index to 'false' in the 'configs/purity.php' file, Purity's internal exceptions will no longer be suppressed, allowing for direct error handling. ```PHP // configs/purity.php 'silent' => false, ``` -------------------------------- ### Adding Sortable Trait to Laravel Model (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/guide/basic-usage.md This snippet shows how to include the `Sortable` trait from `Abbasudo\Purity` in a Laravel Eloquent model. Incorporating this trait enables the model to utilize Purity's sorting functionalities, allowing for dynamic data sorting based on request parameters. ```php use Abbasudo\Purity\Traits\Sortable; class Post extends Model { use Sortable; // } ``` -------------------------------- ### Generating Custom Filter Class - Shell Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/custom.md This shell command uses Laravel Artisan to generate a new custom filter class named `EqualFilter`. The generated class will be placed in the `Filters` directory, which is the default location for custom filters in the Laravel Purity package. ```sh php artisan make:filter EqualFilter ``` -------------------------------- ### Configuring Global Filters in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This snippet demonstrates how to define global filters in the `configs/purity.php` file. These filters are applied by default to all queries unless overridden. It specifies `EqualFilter` and `InFilter` as allowed global filters. ```PHP // configs/purity.php 'filters' => [ EqualFilter::class, InFilter::class, ], ``` -------------------------------- ### Overwriting Allowed Fields in Controller - PHP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/allowed.md This snippet shows how to dynamically overwrite the allowed filter and sort fields directly within a controller using the `filterFields` and `sortFields` methods. These methods provide a way to set allowed fields on the fly, taking precedence over any fields defined in the model. ```php Post::filterFields('title', 'created_at')->filter()->get(); Post::sortFields('created_at', 'updated_at')->sort()->get(); ``` -------------------------------- ### Configuring Allowed Filter and Sort Fields in Model - PHP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/allowed.md This snippet illustrates how to explicitly define the allowed fields for filtering (`$filterFields`) and sorting (`$sortFields`) directly within a Laravel model. Any fields not specified in these arrays will be rejected by Purity when attempting to filter or sort. ```php // App\Models\User protected $filterFields = [ 'email', 'mobile', 'posts', // relation ]; protected $sortFields = [ 'name', 'mobile', ]; ``` -------------------------------- ### Configuring Custom Filter Location - PHP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/custom.md This PHP configuration snippet, found in `configs/purity.php`, defines the directory where custom filter classes are located. By default, the Laravel Purity package loads all classes from `app_path('Filters')` as custom filters, but this path can be modified here. ```php // configs/purity.php 'custom_filters_location' => app_path('Filters'), ``` -------------------------------- ### Configuring Filterable Fields in Tags Model (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/relation.md This PHP snippet configures the `Tags` model by using the `Filterable` trait and defining the `$filterFields` property. This property specifies which fields (e.g., 'title') of the `Tag` model are available for filtering when queried through a relation. ```php class Tags extends Model { use Filterable; protected $filterFields = [ 'title', ]; } ``` -------------------------------- ### Applying Runtime Filters with filterBy() in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This snippet demonstrates applying filters directly at runtime using the `filterBy()` method on an Eloquent builder. Filters passed to `filterBy()` take the highest precedence, overriding both global and model-defined filters. It shows usage with both string aliases and class names. ```PHP Post::filterBy('$eq', '$in')->filter()->get(); // or Post::filterBy(EqualFilter::class, InFilter::class)->filter()->get(); ``` -------------------------------- ### Defining HasMany Relation for Sorting in Post Model (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/relation.md This PHP snippet defines a `hasMany` relationship named `tags` in the `Post` model, linking it to the `Tag` model. It uses the `Sortable` trait, enabling Purity to sort `Post` records based on associated `Tag` fields. ```php use Illuminate\Database\Eloquent\Relations\HasMany; class Post extends Model { use Sortable; public function tags(): HasMany { return $this->hasMany(Tag::class); } } ``` -------------------------------- ### Defining Eloquent Relation for Filtering - PHP Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/allowed.md This snippet demonstrates how to define an Eloquent `HasMany` relation in a Laravel model. For Purity to automatically allow filtering on related models, it requires a defined return type for the relation, as shown here. ```php // App\Models\User public function posts(): Illuminate\Database\Eloquent\Relations\HasMany // This is mandatory { return $this->hasMany(Post::class); } ``` -------------------------------- ### Applying Runtime Field Restrictions with restrictedFilters() (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This PHP snippet shows how to apply field-level filter restrictions directly on the Eloquent builder using the `restrictedFilters()` method. This method has the highest priority, overriding any restrictions defined in `$filterFields` or `$restrictedFilters` properties. It allows dynamic restriction of fields like 'title' to specific operators. ```PHP Post::restrictedFilters(['title' => ['$eq']])->filter()->get(); ``` -------------------------------- ### Understanding Field Restriction Limitations in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This snippet illustrates a critical limitation of field-restricted filters in Purity. It shows that a field operation (e.g., '$eqc' for 'title') cannot be restricted if the base filter itself ('$eqc') is not already permitted in the model's `$filters` property. This ensures consistency and prevents unauthorized operations. ```PHP $filters = ['$eq']; $restrictedFilters = ['title' => ['$eqc']] // This won't work ``` -------------------------------- ### Configuring Null Values Last in Laravel Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/sort/null-sort.md This configuration setting in `configs/purity.php` ensures that when a column containing null values is sorted, these nulls will always appear at the end of the result set, regardless of whether the sort order is ascending or descending. ```php // configs/purity.php null_last => true; ``` -------------------------------- ### Overwriting Renamed Filter and Sort Fields in Laravel Purity Controller (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/rename.md This snippet shows how to dynamically overwrite the $renamedFilterFields and $sortFields defined in the model directly within a controller or query chain. It uses the renamedFilterFields() and sortFields() methods on the model instance to apply temporary field renaming for specific queries. ```php // Overwriting the renamed filter fields in the controller. Post::renamedFilterFields(['created_at' => 'published_date'])->filter()->get(); // Overwriting the sort fields in the controller. Post::sortFields([ 'created_at' => 'published_date', 'updated_at' ])->sort()->get(); ``` -------------------------------- ### Defining HasMany Relation for Filtering in Post Model (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/relation.md This PHP snippet defines a `hasMany` relationship named `tags` in the `Post` model, linking it to the `Tag` model. It uses the `Filterable` trait, enabling Purity to filter `Post` records based on associated `Tag` fields. ```php use Illuminate\Database\Eloquent\Relations\HasMany; class Post extends Model { use Filterable; public function tags(): HasMany { return $this->hasMany(Tag::class); } } ``` -------------------------------- ### Restricting Filters by Field using $filterFields (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This PHP snippet illustrates the first method for restricting allowed filters per field using the `$filterFields` property. It defines which operators are permitted for specific fields, such as limiting the 'title' field to the '$eq' operator. This method requires explicitly listing all allowed fields. ```PHP $filterFields = [ 'title' => ['$eq'], // title will be limited to the eq operator 'title' => '$eq', // works only for one restricted operator 'title:$eq', // same as above 'title', // this won't be restricted to any operator ]; ``` -------------------------------- ### Defining Model-Specific Filters in Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/filter/restrict.md This PHP snippet shows how to declare filters directly within a Laravel model using the `$filters` private array. These model-specific filters override any global filters defined in the configuration file. It illustrates using both string aliases and class names for filters. ```PHP // App\Models\Post private array $filters = [ '$eq', '$in', ]; // or private array $filters = [ EqualFilter::class, InFilter::class, ]; ``` -------------------------------- ### Renaming Sort Fields in Laravel Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/rename.md This snippet illustrates how to rename sort fields using the $sortFields property in a Laravel model. It allows mapping client-side sort names (e.g., 'phone') to actual database column names (e.g., 'mobile'), enabling clients to sort by a different name. ```php // App\Models\User // Example URL: ?sort=phone // The $sortFields property is used to map the client-side field names to the actual database column names for sorting. protected $sortFields = [ 'name', 'mobile' => 'phone', // The actual database column is 'mobile', but the client should use 'phone' for sorting. ]; ``` -------------------------------- ### Renaming Filter Fields in Laravel Purity (PHP) Source: https://github.com/abbasudo/laravel-purity/blob/master/docs/advanced/rename.md This snippet demonstrates how to rename filter fields using the $renamedFilterFields property in a Laravel model. It maps client-side field names (e.g., 'phone') to actual database column names (e.g., 'mobile') or relation names (e.g., 'writing' to 'posts'), allowing clients to use a different name for filtering. ```php // App\Models\User // Example URL: ?filter[phone][$eq]=0000000000 // The $renamedFilterFields property is used to map the client-side field names to the actual database column names. protected $renamedFilterFields = [ 'mobile' => 'phone', // The actual database column is 'mobile', but the client should use 'phone'. 'posts' => 'writing', // The actual relation is 'posts', but the client should use 'writing'. ]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.