Sometimes you may wish to use custom pivot model when working with many-to-many relationships in Eloquent, in order to add some behaviour on top of the core features.
There are a few things to consider before you start.
Eloquent provides one Relation type for far related tables –
hasManyThrough. However it works only with with cascade of
hasOne/hasMany relations and you can use it only for 2 levels of nesting, while sometimes you need more.
Imagine, that you want to get the posts for logged-in user, having this setup:
// User subscribed to many tags
User belongsToMany Tag
// Post migh be tagged by many tags
Tag belongsToMany Post
Now, because there is no built-in method for such case, you would probably think of getting all the
tags, looping through them, and merging all the
posts from each tag. While this would work, it would be cumbersome, so let’s make it easier!
After spending long time with Eloquent I finally decided to release some of my work – extensions that will make your life easier, when working with Eloquent ORM.
They will be released bit by bit on github and much more will be found in the book I’m working on.
As a starter take a look at the repo and make sure to follow, for I will be updating it regularly!
Leave your thoughts in the comments – what would you like me to cover in the book, what is the most difficult part that you need help with, what use-cases would be most desirable.
Eloquent Global Scopes make it easy to customize your Model’s queries, however sometimes it is just not enough.
You may want to use similar solution for you
DB::table(..) calls as well. While there’s no built-in feature for this, you can still do it with just a little more effort, and here’s how it’s done.
It is essential for every application to keep track of the data revisions. As an admin or sort of supervising user, you want to know how, when and who is responsible for changing the data.
Here’s something you should try out, because it makes this kind of work stupid-simple:
When working with relationships, most likely you need to get count of related models in many places. There are obvious ways to achieve this of course, but not always efficient. Especially when you are loading a collection of models and their relations.
So let me show you what we can do about it!
Laravel 4.2 introduced a new way of handling
soft deletes by making use of
traits and query builder
macros wrapped in as a feature, that many people asked for for a very long time – Global Scopes.
This might be very useful for everyone, however the docs are not very eloquent in this case. So keep on reading to learn how you can implement global scopes in your project – it’s really easy and lets you write even more expressive code for your Eloquent models.
Check the ready-to-use demo at …
Previously I showed you how to get single related model per each parent, while eager loading the
hasMany relation. That was pretty easy and we just made the most of the
However, most likely the time will come, when you need several related models per parent.
It won’t be as easy, but it’s far from impossible, so let’s do it!
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. Continue reading