Posts tagged with eloquent

Extending models in Eloquent

Caleb Porzio, co-presenter of the Twenty Percent Time podcast, published a new article on the Tightenco blog. This time he guides us through a nice use case for extending Eloquent models.

Let’s explore another alternative that can be used as a stand-in for repetitive where statements and local scopes. This technique involves creating new Eloquent models that extend other models. By extending another model, you inherit the full functionality of the parent model, while retaining the ability to add custom methods, scopes, event listeners, etc. This is commonly referred to as “Single Table Inheritance,” but I prefer to just call it “Model Inheritance”.

https://tighten.co/blog/extending-models-in-eloquent

Read more

Join thousands of developers

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages.

No spam. Unsubscribe anytime. You can also follow me on X.

Tweaking Eloquent relations – how to get latest related model?

Jarek Tkaczyk demonstrates how you can use a helper relation to eager load specific models.

Have you ever needed to show only single related model from the hasMany relationship on a set of parents?

Being it latest, highest or just random, it’s not very clever to load whole collection using eager loading, just like running query per every parent.

Of course you can do that better, and now let me show you how.

https://softonsofa.com/tweaking-eloquent-relations-how-to-get-latest-related-model/

Read more

How (not) to use accessors in Eloquent

Jarek Tkaczyk wrote a blogpost on the usage of accessors in Eloquent. He demonstrates what could go wrong when using accessors on certain fields.

The moral of the story is, that data handling and its presentation should not go into the same bucket. And the model is that bucket – instead of creating accessors, traits or god knows what for this task, better use decorator (like) pattern where you can do all the necessary work for preparing your data to be displayed in your view, without touching actual values anywhere else.

https://softonsofa.com/they-can-bite-how-not-to-use-accessors-in-eloquent/

Keep this in mind when you should use my pragmatic approach for presenters. Watch out for conflicting names.

Read more

Writing advanced Eloquent search query filters

Over at the excellent dotdev.co Amo Chohan wrote an article on how to structure code when you need many filters on an Eloquent model.

I recently needed to implement a search feature in an events management project I was building. What begun as a few simple options (searching by name, e-mail etc), turned into a pretty large set of parameters. Today, I’ll go over the process I went through and how I built a flexible and scalable search system. For those of you who are eager to see the final code, head over to the Git repository to see the code.

https://dotdev.co/writing-advanced-eloquent-search-query-filters-de8b6c2598db

Read more

How is Doctrine 2 different to Eloquent?

One of the really great things about ORM’s that implement the Active Recordpattern like Eloquent is, they are really easy and really intuitive to use.

With Active Record, you basically just have an object that you can manipulate and then save. Calling save() on the object updates the database, and all of the magic persistence happens behind the scenes.

Having the business logic of the object and the persistence logic of the database tied together definitely makes working with an Active Record ORM easier to pick up.

In this tutorial I want to explore exactly how Doctrine 2, an ORM that implements the Data Mapper pattern, is different to Eloquent.

http://culttt.com/2014/07/07/doctrine-2-different-eloquent/

Read more