Category: Laravel, PHP

You have probably faced the situation where you needed to filter your query based on given parameters in url query-string and after developing the logics, You’ve had a code-base like what we can in the above image. When the number of parameters starts to grow, The number of these kind of if statements also grows and your code gets huge and hard to maintain. Also it’s against the Open/Closed principal of SOLID principles, Because when you have a new parameter, You need to get into your existing code and add a new logic (which may breaks the existing implementations).

It's the proper filter when you need to directly filter one of your table's columns.

Output: In order to prevent your model to get messy or populated with filter methods, You can create a trait for it and put everything about filters inside the trait.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Community Partners