Custom Laravel Eloquent Collections – Fast tips

Valerio Barbera

Eloquent is one of the most powerful components of the Laravel framework. It’s an Object-Relational Mapping (ORM) tool that simplifies database interactions. It provides a convenient way to work with database records through its built-in collections. While Laravel comes with a variety of pre-defined collection methods, you can also extend the basic Illuminate\Database\Eloquent\Collection class to simplify recordsets manipulation.

In this tutorial, we will explore how to create and use custom Laravel Eloquent collections, with practical examples to demonstrate their usefulness.

Understanding Laravel Eloquent Collections

Before diving into custom Eloquent collections, let’s briefly understand the concept of what they are. Eloquent collections are PHP arrays with additional helper methods that provide a convenient way to work with sets of Eloquent model records.

They offer a range of useful methods for filtering, mapping, sorting, and manipulating data.

Creating a Custom Eloquent Collection

To create a custom Eloquent collection, you need to extend the base Illuminate\Database\Eloquent\Collection class provided by Laravel. This base class provides several helper methods and features that can be leveraged when creating your custom collection.

use Illuminate\Database\Eloquent\Collection;

class CustomCollection extends Collection
{
    // Your custom collection methods goes here
}

To make your custom Eloquent collection more powerful, you can add your own methods. These methods can be specific to your application’s domain or offer additional convenience when working with collections.

This strategy can allows you to incapsulate certain logic in a single class that can be reused across the application.

Real life example of Custom Eloquent Collection

Let’s consider an example of a booking system where we want to resort the internal items according to specific characteristics, but it is much easier to do at code level instead than SQL.

namespace App\Collections;

use App\Models\Room;
use Illuminate\Database\Eloquent\Collection;

class AvailableRoomsCollection extends Collection
{
    public function __constructor($data)
    {
        $this->sortBy(function (Room $room) {
            // Apply sorting logic
        });
    }
}

Or you can add a specific method to be called externally.

namespace App\Collections;

use App\Models\Room;
use Illuminate\Support\Collection;

class AvailableRoomsCollection extends Collection
{
    public function sortByAvailability()
    {
        return $this->sortBy(function (Room $room) {
            // Apply sorting logic
        });
    }
}

Since your custom collection is automatically sorted when created, you can use it in one or more controllers with the convenience of having the sorting logic encapsulated in the collection itself:

namespace App\Http\Controllers;

use App\Collections\AvailableRoomsCollection;
use App\Models\Room;
use Illuminate\Http\Request;

class AvailabilityController
{
  public_function index(Request $request)
  {
    return new AvailableRoomsCollection(Room::filter($request->filter)->get());
  }
}

You can also implement the newCollection method directly in the Room model class to automatically get the custom colleciton instance as a query result:

use App\Collections\AvailableRoomsCollection;
use Illuminate\Database\Model;

class Room extends Model
{
    ...

    /**
     * Create a new Eloquent Collection instance.
     *
     * @param  array  $models
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function newCollection(array $models = []): AvailableRoomsCollection
    {
        return new AvailableRoomsCollection($models);
    }

    ...
}

You can simplify the Controller even more by returning the result of the query directly.

class AvailabilityController
{
    public_function index(Request $request)
    {
        return Room::filter($request->filter)->get();
    }
}

Custom Laravel Eloquent collections provide a way to extend the functionality of the default collection methods and add convenience methods and behaviours tailored to your application’s needs. By creating custom collections you can streamline your codebase.

You can follow me on Linkedin or X. I post about building my SaaS business.

Monitor your Laravel application for free

Inspector is a Code Execution Monitoring tool specifically designed for software developers. You don’t need to install anything on the infrastructure, just install the Laravel package and you are ready to go.

Inspector is super easy to use and require zero configurations.

If you are looking for HTTP monitoring, query insights, and the ability to forward alerts and notifications into your preferred messaging environment try Inspector for free. Register your account.

Or learn more on the website: https://inspector.dev

Related Posts

Neuron AI Laravel SDK

For a long time, the conversation around “agentic AI” seemed to happen in a language that wasn’t ours. If you wanted to build autonomous agents, the industry nudge was often to step away from the PHP ecosystem and move toward Python. But for those of us who have built our careers, companies, and products on

PHP’s Next Chapter: From Web Framework to Agent Framework

I’ve spent the last year building Neuron, a PHP framework designed specifically for agentic AI applications. What started as a technical challenge became something else entirely when developers began reaching out with stories I wasn’t prepared to hear. They weren’t asking about framework features or deployment strategies. They were telling me about losing their jobs.

Storing LLM Context the Laravel Way: EloquentChatHistory in Neuron AI

I’ve spent the last few weeks working on one of the most important components of Neuron the Chat History. Most solutions treat conversation history in AI Agents forcing you to build everything from scratch. When I saw Laravel developers adopting Neuron AI, I realized they deserved better than that. The current implementation of the ChatHisotry