Ask Johno Laravel Tutorials

Model Search and Filtering in Laravel

Hi guys, as I have written this functionality a few times at this point, and I have been asked how to do it a couple of times, too. That is Laravel searching and filtering, I am going to develop a simple composer package which I can use to allow users to search and filter Laravel (Eloquent) Models using PHP traits which can be used on Laravel controllers.


I am going to work on the assumption that you are comfortable with basics.

I shall also assume you’re fairly comfortable with the Illuminate HTTP Request object, a Controller, and Eloquent Model.


So the first thing we want to tackle, as a concept, is that the best way, in my experience, is to start with a query, and then modify it as required. This allows us to optionally do almost anything, but equally allows us to do nothing, and simply return an unfiltered set if we want to.

This point would be true outside of Eloquent too, even if working with vanilla SQL.

Starting Off

To start with you want something like this within your Controller.

public function index(Request $request)
// Of course you can change this if you don't want to select everything
$query = YourModel::select('*');

Now we have the $query object which we can work with as and where we want to.

Filtering Records

Excluding deleted/is deleted, as we’ll cover that later on.

Let’s say you now want to filter results on author_id just to keep things simple.

$query->where('author_id', '=', $request->author);

Of course take into account everything here, you may need to do sanitisation or validation or anything else in there.

Multiple Filters, Save Lines

If you have many fields on which you wish to do this I usually find something like this is better

$fields = ['author_id', 'category_id'];
foreach($fields as $field){
$query->where($field, '=', $request->$field);

That example assumes the exposure of your field names, you could always use an associative array so you could do “author” maps to “author_id” and so on, if you were so inclined.

Fluffy Search

Now you might want to do a fluffy search, taking the assumption of posts with content and an author name, and a category name (stored within the model) you could do something like this

$searchFields = ['title','content','author_name','category_name'];
$query->where(function($query) use($request, $searchFields){
$searchWildcard = '%' . $request->search . '%';
foreach($searchFields as $field){
$query->orWhere($field, 'LIKE', $searchWildcard);

Handling Trash

You probably want something which allows you to see soft deleted models, if you’re using them. I do something like this

if($request->trash == true){
// Retrieve soft deleted models only, you can only do this if you're using soft deletion on this model

Ordering and Limiting

Now to handle ordering and pagination – which Eloquent beautifully does for us, all we need to do is let the user modify it if they want to.

// Set the query ordering
$query->order($request->orderBy ?? 'updated_at', $request->order ?? 'DESC');

$perPage = $request->per_page ?? 25;

// It is a good idea to make sure we cap this
if($perPage > $maximumPerPage){
$perPage = $maximumPerPage;

// Final part - get the results
$results = $query->paginate($perPage);

// You will now need to do something with your results, and return some kind of Response - this could be a JSON response or adding the results to the data returned to the view, depending on your context

There’s an assumption here that our default order is updated at, last updated first, and that if not specified, we want to return 25 pages.

Of course you can store this information wherever you want.

In Summary, and Further Reading

Rather than creating a really complicated set of rules to handle all the things you may need from your API calls, start with a query, and then optionally modify that query.

Further Reading


Quick Life Hacks with the Eloquent Model

Hi folks,

It’s no secret that I love Laravel, and especially love Eloquent. This post isn’t about why various people don’t like Eloquent, etc, etc. but just some “life hacks” of working with Eloquent.

#1 – Naming Conventions

I personally like to use the following naming convention on my databases (using a forum as an example)


Whereas Eloquent would expect the following:


I personally dislike this, for no other reason than I like to be able to see what’s being owned by what at a very quick glance. Additionally, namespacing; I like to namespace my Models, at the very least into a Model namespace, but often I like to further namespace these to represent the ownership of the data; so I end up with something like (I’m using a directory structure, because PSR-4 namespaces follow this anyway)

- Model/
- - Forum
- - ForumTopic
- - ForumTopicReply

My only issue with this is that the Model namespace can end up huge, and with classes with some really ridiculously long names. So I prefer to break this down like so:

- Model/
- - Forum
- - Forum/
- - - Topic
- - - Topic/
- - - - Reply

I realise this begins to look like overkill, but I find it particularly useful when separating functionality by module or something like that.

The Autonaming

The above means I have two options, number one is to follow the convention to the letter. Option two is to overwrite the naming convention on a base model, from which I then extend. This base/abstract model contains the following method:

 * Overwrite the autonaming conventions
 * @return string
public function getTable()
    $class = substr(get_class($this), strlen(self::MODEL_NAMESPACE));
    $namespaces = explode('\\', $class);
    foreach($namespaces as &$namespace){
        if(substr($namespace, -1) == 'y'){
            $namespace = strtolower(substr($namespace, 0, -1) . 'ies');
        } else {
            $namespace = strtolower($namespace . 's');
    return implode('_', $namespaces);

Note: You will need to have the MODEL_NAMESPACE constant declared as well

All this does is turned app\Model\Forum\Topic into forums_topics, but it’s pretty useful

PSR-2 properties

So the other thing I find occasionally annoying is that when writing PHP my properties follow the PSR-2 naming convention (camelCaseForVariables) – but using eloquent I am forced to snake_case_columns because (understandably) this is what is in the database.

By adding the following 3 methods to your base model you can use PSR-2 compliant field names. So that created_at becomes createdAt; without having to modify the database;

 * Overwrite the getter so that we can use camelCase field names
 * @param string $property
 * @return mixed
public function __get($property)
    return parent::__get($this->convertToSnakeCase($property));

 * Overwrite the setter so that we can use camelCase field names
 * @param string $property
 * @param mixed $value
public function __set($property, $value)
    parent::__set($this->convertToSnakeCase($property), $value);
 * Turn a camelCase string into snake_case
 * @param $string
 * @return mixed
protected function convertToSnakeCase($string)
    return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $string));

Quite simply, all these methods are doing is plugging into the original getter and setter, but first converting camelCase to snake_case – so that throughout my code I can refer to these fields in a way which is fitting with the rest of my code, which conforms to PSR-2

There you have it

A couple of very quick, but very useful, ways to extend the base Eloquent Model so that it is in better fitting with the rest of your code base and database standards (or mine, anyway)

General Laravel

How Eloquent breaks the Single Responsibility Principle, and why I’m okay with it

Hi everyone

I thought I would discuss a controversial one today, in order to give an articulated response to a common argument about Eloquent (and generally the Active Record pattern)

Firstly, just in case you don’t know, here are some definitions:

Single Responsibility PrincipleThe “S” in the “SOLID” principles. In short, this means one class should have one job. The idea is to keep your code modular and easy to switch out.

SOLID Principles: I’ll do an article on this one day, but it’s a discussion for another time. Like all principles or methodologies they’re like the Pirate’s Code, they’re more like guidelines than hard and fast rules of software design.

Eloquent: Is an ORM (object relational model) which comes as standard with Laravel, and is absolutely fantastic

Active Record: Is a design pattern whereby a record from a database table is returned as an object that can be manipulated and can update the database as part of its role. Usually this means you can do something like

$myRecord = RecordFactoryOrSomething::getTheRecord();
$myRecord->field = 'Updated Value';

This is quite handy for programming as you can store all of the logic to do with saving in the Model.

Laravel: Is a framework for Rapid Application Development, which, like Eloquent, is amazing. It basically gives you all of the tools you need to build bespoke software, but in a way that means you can do it at lightening speed, with crazy accuracy. I can’t sell Laravel well enough, honestly it’s amazing.

So, now that the introductions are out of the way; the Single Responsibility Principle isn’t mandatory

I’d like to make my first point, which quite simply is: The Single Responsibility Principle is a principle. I joked earlier about principles being like the Pirate’s Code, but one does have to be careful when using design patterns and principles, if we’re not careful they become anti-patterns which essentially means their over usage and religious subscription actually causes more problems in the software than it would’ve done had it have been omitted completely.

All patterns should be used in a way which makes sense and makes the code more efficient and maintainable. Ultimately maintainable code which is easy to extend is cheaper for the business whose employing you. Or means your profit margin is higher if you’re running your own business, freelancing or contracting.

Single Responsibility is pretty subjective

This is another slight issue I have with the Single Responsibility Principle. I actually love the principle itself. It makes sense, separate things into logical blocks, and if each class has its own responsibility (and only the one) then it’s quite easy to find how the classes are supposed to work together, perfick.

However, a single responsibility has to be defined. If you defined the single responsibility of a class as “do the business logic” then you could have a God class, which technically has a single responsibility. That would, obviously be a bad usage.

So, let’s look at the other end of the spectrum and go into tiny, tiny detail. Do I need a class for executing SQL queries, and one for generating them? Maybe that means I need a class which prepares INSERT statements, another for UPDATE, and so on and so forth.

The latter approach to single responsibility is most definitely an anti-pattern. That level of abstraction and obscurification will only ever be bad for managing and debugging code.

So then how does the Eloquent Model break it?

The big problem people have with Eloquent is that the Model class fulfils multiple responsibilities. You can’t really say “it is responsible for interacting with a specific table” because it is also representational of a record.

That means it has, at least, two responsibilities: Executing queries against a table, and representing a record. The power of the Eloquent Model is that it actually does more than that, it also defines relationships etc.

So, let’s be honest, it rips the Single Responsibility Principle apart, but I’m totally okay with that.

But, if you love SRP, how can you like the Eloquent Model, you heathen?

The reason I love it, is because what you actually do is group those few responsibilities in an entirely sensical and logical place, you end up with a class which can give you lots of information; all of those responsibilities can be so beautifully wrapped; as so you end up with something like this:

NB: I’ve omitted a lot of code here, just to demonstrate my point I didn’t need it

class User extends Model

    // This is a query run against the table
    public static function findUserOnEmail($emailAddress){}

    // This is an action (probably running a query)
    public function changeUserLevel($userLevelId){}

    // This is a relationship defined
    public function friends()
        return $this->hasMany('\ModelNamespace\Friendship');


And there you have it, in a few lines of code we have some functionality we need on the table to complete search functionality which has to otherwise be stored somewhere else, that usually doesn’t make sense.

You’ve got an action that can be publicly exposed to other developers to carry out a task without any prior knowledge of the data architecture of your system or how the user levels.

And you’ve got an easy way to get all of the friends for that user.

This is why I love Eloquent. Because it puts aside rules and methodologies for a logical and sensical reason, in order to provide an exceptionally transparent way to manage your data layer.

But wait, there’s more!

All of this is without things like the hookable functions so you can do certain functionality (think expire memcached objects, etc) based on creation, update, or save. There’s a huge amount of functionality built in, and it’s perfect for almost everything.

I’ll be honest, all ORMs fall down with certain complex queries – but it’s not supposed to tackle that problem. It’s the vast majority of monotonous tables and relationships which have to be constantly created to support any kind of enterprise software which becomes so much faster.

Alright, so what’s the alternative?

The alternative to this, realistically, is the Service-Mapper-Entity pattern. In short this means each table has an Entity responsible for representing the data. You then have a Mapper responsible for executing SQL, and then you have a Service which is the interface exposed to the rest of the application. So you tell your service to do something, and it does this using the Mapper, usually returning Entities.

Personally, that feels like too much code to do a simple task. And it is just a personal opinion and there are times that this is very useful, the thing that makes me really sway from that is that when I create 5 tables, with, let’s say 3 bridging tables to support many-to-many relationships; I suddenly can end up with 8 sets of Service, Mappers and Entities. This is 24 classes, which is a lot, compared to 8 classes; which define their relationships and are ready to be used with hydration and all of that stuff.

So, it’s okay to break the Single Responsibility Principle?

Before I ignite an absolute flame war, which this particular topic has the potential to do. In a word, yes. But only if you have a good reason, and you’ve done it to make your code maintainable, readable and easy to manage; without sacrificing security or anything like that.

In this particular instance, I think breaking the principle is fine. It makes sense, and it makes for, even on large scale applications with a large data layer, an easy way of managing this data layer and managing it in a way which is sustainable moving forward.

And how do I keep it clean?

For me personally, follow two major rules.

  1. Static methods do something to the table itself – this could be searching, filtering, retrieving, etc.
  2. Methods on the class, do something to the record – I often like to wrap common functionality in a method, it makes it easily accessible to everyone else

For example; let’s say we activate a user account, there are two ways of approaching this:

// Fetch the record with ID of 17
$record = User::find(17);

// Set the is_user_active field to 1
$record->is_user_active = 1;

// Save this record

There isn’t necessarily anything wrong with this. Until I want to hook into that functionality, or I change something else, let’s say I nullify the activation_key field, for example. Then I would have to go and find everywhere I’ve done this. I don’t like this, it’s a bit awkward.

So I prefer to do something like this

// Fetch the record with ID of 17
$record = User::find(17);

// Activate the record

// Within your User model class
public function activate()
    $this->is_user_active = 1;

Disclaimer: You can do this using the updated method on in Eloquent, so you could hook into this and check to see if the is_user_active field has changed and then hook in that way; word of caution with that though – you can end up with a hell of a lot of if statements and it can get quite messy.

In summary, Eloquent is awesome

  1. It let’s you have an easy to manage data layer for your application
  2. The Model class does break the Single Responsibility Principle, but it can be argued that Model class is responsible for representing and interacting with a single table
  3. You end up with easy to plug into user-land code
  4. You can manage your relationships beautifully

So really, when weighing that up I can’t see any reason not to use it. It’s extendible (I’ve extended the base Eloquent Model multiple times, with a lot of success), you can overwrite functionality where you want to really easy – though you don’t often need to, because the functions you can plug into are so useful.

As always I’d be really interested to hear any thoughts you guys have on this, especially as I know it’s a bit of a controversial one!