Quantcast
Channel: PHPDeveloper.org
Viewing all articles
Browse latest Browse all 12

Michael Dyrynda: Filtering models with Eloquent in Laravel

0
0

Michael Dyrynda has a recent post about handling matching and limiting results in Eloquent models in a Larvel-based application.

Say you have a users table with the following fields in it name, email, city, state, zip. You may want to provide fuzzy searching for the name, email, or city and exact matching for the state and zipfields. Why fuzzy matching for only some of the fields? Well, you might want to search for everyone whose name contains Michael or has has an @gmail.com address. Be mindful of the latter; it will expose a large dataset if you're not careful in restricting access to the functionality. You probably wouldn't want to allow it in anything bigger than a proof of concept (which this is!).

He goes through the model process, showing how to set up a simple model with the fields mentioned and make use of query scopes to limit returned results. Code is included showing how to define the "scopeFilter" method in the model and call the "User" model instance with the "filter" method. The example limits the results to only the users with a value in the "name" and "state" field.

Link: https://iatstuti.net/blog/filtering-models-with-eloquent-in-laravel

Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images