DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Build Your Exchange Server Recovery Strategy to Overcome Ransomware Attacks
  • Manage Microservices With Docker Compose
  • Build a Java Microservice With AuraDB Free
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify

Trending

  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • Rust and WebAssembly: Unlocking High-Performance Web Apps
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Clean Up Event Data in Ansible Event-Driven Automation
  1. DZone
  2. Data Engineering
  3. Databases
  4. Build Dynamic Filter Controls in Laravel

Build Dynamic Filter Controls in Laravel

In this post, a developer shares a quick tutorial on how he creted a filter control bar for a web app in PHP using Laravel.

By 
Adi Sk user avatar
Adi Sk
·
Mar. 01, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
21.2K Views

Join the DZone community and get the full member experience.

Join For Free

Hi all, this is Adi.

I wanted to share with you my simple solution for searching, filtering, and sorting data in Laravel.

The final widgetHere’s an image of what we are going to build. The first input is for searching, the second input is to select the 'order by' column, the third is to select how to sort, and the last dropdown we have is to choose how many items we want in a page.

You can find all the code for this tutorial at this GitHub Gist.

TL;DR

My solution is quite simple, both the UI part and the controlling logic. The UI part is a horizontal form with a GET method to the same page, this way, when the Filter button is clicked, the form is redirected to the current page with each input field as query parameters.

The logic in the controller is also quite simple. We check for these query parameters and use them to filter data from the model accordingly.

This is the gist of my solution. I go into much more detail with code examples below.

The Controller

Let’s start with the controller. The logic here is pretty simple.
Controller Logic
As you'll notice, we initialize four variables with default data. Then we check if the request has any parameters, and, if so, we then set the parameters as the variables’ data.

Once we are done checking and setting the parameters, we search, sort, and paginate the user model (in this case, it can be any model). Then we send the filtered data to the View along with other variables.

I’ll cover the search method of the user model in the next section.

The Model

Our search logic is also quite simple. The search method goes inside your user model class.
Model's search method
You can learn more about Laravel Model scopes here.

The View

Like the other parts, the view that makes all this work is also pretty simple.
View markup

We have a form with five elements: the first input gets the search query, the second selects the column to sort by, the third selects the order in which to sort by, the fourth selects the records per page, and the last one is the Filter button to fire off the form request.

Conclusion

I hope you understood this not-so-lengthy tutorial. This is a simple solution I cooked up in minutes for a simple need I had. Hope you make use of it in your projects. If you have improvements to this code, do let me know, I would love to learn them.

Filter (software) Laravel Build (game engine) Database

Published at DZone with permission of Adi Sk. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Build Your Exchange Server Recovery Strategy to Overcome Ransomware Attacks
  • Manage Microservices With Docker Compose
  • Build a Java Microservice With AuraDB Free
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!