15

Extend Laravel Eloquent Collection Object

According to Laravel, all multi-result sets returned by Eloquent, either via the get method or a relationship, will return a collection object. This object implements the IteratorAggregate PHP interface so it can be iterated over like an array. However, this object also has a variety of other helpful methods for working with result sets. For example, we may determine if a result set contains a given primary key using the contains method:

You can also use something like this:

Or you can get a single item from the collection using it’s index like:

Well, the base class for this collection object is \Illuminate\Support\Collection.php and this class contains a bunch of methods that could be applied on a collection object and there is another class \Illuminate\Database\Eloquent\Collection.php, this class extends the base collection class and this child class also contains some useful methods to use with a collection object. When we call something like $users = User::all() we get a collection object and this collection object is an instance of \Illuminate\Database\Eloquent\Collection, which extends the base collection class \Illuminate\Support\Collection.php.

Well, Laravel is really a powerful and flexible framework and it has provided us so many ways to extend the core functionality. In this case, if we want to add more methods in collection object we can do it by extending the \Illuminate\Database\Eloquent\Collection.php class and overriding the newCollection method of \Illuminate\Database\Eloquent\Model.php class in our Eloquent Model. Actually, this Model.php is responsible for the Eloquent ORM and this class contains a public method, which is:

This method returns a collection object and we can override this method in our Eloquent Model and we can return a custom collection object with our custom methods in it by extending the \Illuminate\Database\Eloquent\Collection.php class. To do this, we have to create a class at first, so we can create a class by extending \Illuminate\Database\Eloquent\Collection.php class:

Now if we create an Eloquent Model like:

Now, if we use Order::all()->foo() then it’ll call the method in our CustomCollection because we have returned our CustomCollection class which contains this method. This way we can add our custom methods and can apply those methods on any Eloquent collection object.

I’ve done this, added some custom methods to collection object by extending the collection class and works very fine. This is the step by step process that I’ve done, you may follow and try it, at first create the customCollection class, I’ve put it in a new directory inside app folder:

In my composer.json file I’ve added, "app/extensions" in autoload > classmap section like:

Then I’ve created a BaseModel class in my app/models folder and all of my Eloquent models extends this base class:

So, all of my Eloquent models extends this base model and I can use my customCollection class’ methods in any model. For example, I’ve a User model like:

So, I can now use User::all()->toTable(), here toTable() method is added an my customCollection class and what it does is, just generates a HTML table. How do I use it:

In my user/index.blade.php view file:

Which outputs something like this:
blog_pic1

So, this toTable() method is just a custom method for easily generating a table with an eloquent collection, well, this is just an example of adding more methods in collection object and nothing else.

BTW, if anyone interested in my custom toTable() method then it could be used in alternative ways too:

I’ve added other methods, just for some fun with power of Laravel

This is another dynamic method getWhere($value, $key), using this method I can get all the records from a collection where a given attribute matches a value, for example, $users->getWhereFirstName('Mr') and this method requires the __call() magic method, here it is:

SO, I can use $users->getWhereUsername('user1') or $users->getWhereId(5) and so on.

Well, Laravel has already provided a great collection of methods for the collection object but all of these built in methods are not documented in the online documentation, so by reading the source code in both Collection.php files, (mentioned earlier) we can know more about these methods. Some useful methods are given below:

There are so many built in useful methods available in the collection object, it’s not documented on the online manual but reading the source code will give a clear idea because each method provides commented instruction above them. So, read the code and learn more.

Latest Blog

0

New Union Type in PHP – 8

A new RFC has been finished voting and accepted for PHP – 8. That is “Union Types”, which is an extension of PHP’s type system. […]