Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON to Laravel Eloquent Filters #14

Closed
Thavarshan opened this issue Apr 27, 2024 · 0 comments
Closed

JSON to Laravel Eloquent Filters #14

Thavarshan opened this issue Apr 27, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Thavarshan
Copy link
Owner

Thavarshan commented Apr 27, 2024

Introduce an advanced filter logic system to allow for complex and nested filtering conditions using logical operators like AND, OR, and NOT. This feature would significantly enhance the capability of dynamic query generation based on user inputs, making the filter system more powerful and flexible.

Description:
Currently, the package supports simple key-value pair filtering which is adequate for basic needs. However, for more sophisticated applications such as analytics platforms, complex searches, or user-driven data exploration, there is a need for more complex query capabilities.

Proposed Solution:
Implement a JSON-based filter definition system that allows users to define filters with logical operators and nested conditions. Here is an overview of the proposed feature:

  1. JSON-Based Filter Definitions:
    • Users can define filters using a structured JSON format, allowing for logical combinations and nested conditions. These definitions could be provided via API endpoints, configuration files, or directly within the application code.
  2. Filter Parser:
    • Develop a parser that interprets the JSON input and translates it into SQL conditions or Eloquent query builder statements, handling recursion to support nested conditions.
  3. Custom Query Builder Extensions:
    • Extend Laravel's Query Builder with custom methods such as applyComplexFilter to handle complex logical expressions, ensuring each condition group is applied correctly based on the parsed filter definitions.

Challenges:

  • Performance: Optimize query execution and consider caching strategies for heavy queries.
  • Security: Ensure rigorous input validation and sanitization to prevent SQL injection and unauthorized data access, using Laravel’s parameterized queries and other built-in mechanisms.
  • Usability: Offer clear documentation and API reference to help users utilize complex filters effectively.

Use Cases:

  • E-commerce Platforms: Enable detailed product searches combining attributes like price, categories, brands, and availability. For example, users could filter products that are within a specific price range and belong to multiple selected categories.
  • CRM Systems: Allow users to generate detailed reports or find specific customer segments by applying complex filters to customer data, such as filtering clients based on engagement metrics combined with demographic data.

Additional Notes: This feature would make the filter system a robust tool for developers creating complex and data-intensive applications, providing significant value and enhancing the flexibility of data manipulation.

Example:

{
   "logic": "AND",
   "filters": [
      {
         "field": "price",
         "operator": ">",
         "value": 100
      },
      {
         "logic": "OR",
         "filters": [
            {
               "field": "category",
               "operator": "=",
               "value": "Electronics"
            },
            {
               "field": "category",
               "operator": "=",
               "value": "Clothing"
            }
         ]
      }
   ]
}

In this example, the filter defines a condition where the price is greater than 100 and the category is either "Electronics" or "Clothing". This demonstrates the power of combining logical operators and nested conditions to create sophisticated filters.

Translating JSON to Eloquent Query:

  1. Parse the Root Logic Operator (AND):
    The root of this JSON specifies an AND logic operator, indicating that all contained conditions must be met. In Eloquent, this translates to chaining conditions together using where clauses.
  2. Interpret Individual Filters:
    • The first filter in the array specifies a condition on the price field. This is straightforward:
      ->where('price', '>', 100)
    • The second element contains an OR logic operator, which groups two conditions that can satisfy the query if either is true. In Eloquent, this is handled using the orWhere method, but to ensure it only applies to the conditions within its group, you use where with a closure, inside which you use orWhere:
        ->where(function($query) {
           $query->where('category', '=', 'Electronics')
                 ->orWhere('category', '=', 'Clothing');
        });

Complete Eloquent Query:

Combining these, the complete query built from the JSON looks like this:

$query = Model::query()  // Assuming you are querying a model
   ->where('price', '>', 100)
   ->where(function($query) {
      $query->where('category', '=', 'Electronics')
            ->orWhere('category', '=', 'Clothing');
   });
@Thavarshan Thavarshan added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Apr 27, 2024
@Thavarshan Thavarshan self-assigned this Apr 27, 2024
@Thavarshan Thavarshan pinned this issue Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant