Elegant Way to Filter and Sort Queries in Laravel

Jun 24, 2023 | Programming

Social Card of Laravel Purity

![Tests](https://github.com/abbasudo/laravel-purity/actions/workflows/tests.yml/badge.svg) ![License](http://poser.pugx.org/abbasudo/laravel-purity/license) ![Latest Unstable Version](http://poser.pugx.org/abbasudo/laravel-purity/v) ![PHP Version Require](http://poser.pugx.org/abbasudo/laravel-purity/require/php) ![StyleCI](http://github.styleci.io/repos/603931433/shield)

Laravel Purity is an elegant and yet simple filtering and sorting package for Laravel, designed to simplify complex data filtering and sorting logic for Eloquent queries. By simply adding filter() to your Eloquent query, you can allow frontend users to apply filters based on URL query string parameters with ease.

How Does Purity Work?

Let’s break it down with an analogy. Imagine you are at a library filled with numerous books (your database of posts). If you were to ask the librarian (your Eloquent query) to find a specific book (post), instead of sifting through every single book manually, you can give them precise instructions like: “I’m looking for all books that contain the word ‘Purity’ in the title.” This is essentially what the filter does.

Here is a basic usage example to clarify Purity’s use case:

$posts = Post::filter()->get();

That’s it! Now you can filter your posts by adding query string parameters to the URL. For example, to filter by title, you might use:

GET /api/posts?filters[title][$contains]=Purity

For more detailed instructions, you can read more at the official documentations.

Documentation

You can find the complete documentation for Laravel Purity here.

License

Laravel Purity is Licensed under The MIT License (MIT). Please see the License File for more information.

Security

If you’ve found a bug regarding security, please mail amkhzomi@gmail.com instead of using the issue tracker.

Troubleshooting

  • If you are having trouble with the package not filtering as expected, make sure you have configured your query string parameters correctly in the URL.
  • Ensure that you have included the necessary middleware for query string handling in your Laravel application.
  • Always check for updates or any changes in the documentation regarding the version you are using.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox