This episode is for members only

Sign up to access "Mastering Polymorphic Relationships" right now.

Get started
Already a member? Sign in to continue
Playing
09. One of Many

Transcript

00:00
let's take a look at one of many which isn't really a specific relationship type it really is a modifier it relates to morph
00:08
one which we've already looked at but allows us to get like the latest or the oldest of many things so in our case we are in a really good position to come up with a really good example here i want to iterate through each of the articles like
00:24
we're doing here on the screen but i want to show the latest or the oldest comment or we could fetch a comment under a specific condition so let's go ahead and look at each of these options okay so let's get rid of the iterate loop here i want to grab the latest
00:40
comment and display it on this page how do we do that well let's go over to our article where we have this comments relationship we're not going to lean on this of course what you could do is you could say something
00:54
let's go and get rid of this we could say something like article comments latest body sorry not latest first that would be the scope if we were actually
01:19
querying this and everything like that and we got comment 5 and comment 1 now the reason that we don't want to do this is first of all memory usage what we're doing is we're diving into an entire collection of comments to then just pluck out the first one so this will
01:34
load all of the comments for this article into memory and although we only have a few at the moment this will bump up your memory usage of your app and slow things down if you have say thousands of comments the other reason that it's a good idea to
01:47
create a specific relationship like this is then you can treat it like a relationship so if you need to modify the latest comment of an article or you need to delete the latest comment of an article you can access the relationship and then just say delete we might have a look at
02:01
that in a second okay so this isn't the best solution we've determined that how do we get the latest comment but defined via relationship well we're going to go ahead and we're going to define out a new relationship in here so let's say
02:14
comment and what are we going to do well what is the latest comment it's just one thing isn't it so we're going to say return this morph one and we're going to say comment like we did before so exactly the same
02:28
thing with the same commentable type in here now that's not all we want to do because this relationship now does not make sense what we want to say is we want to use that modifier which i said it's not really sort of like a real relationship
02:41
type it's more of a modifier that we add on here with a custom name so latest of many is going to return a one type so just return one single comment which is what we want we don't want a collection because we just want the latest comment we can use this to access it like a
02:57
normal relationship so we don't need to invoke this we can just use latest comment like an attribute and latest of many or is that modifier that will do the query behind the scenes for us and grab this out we'll be able to see this because we've got lariver debug bar
03:10
open so let's go back over to the home page and let's say comment or article in this case latest comment like so so remember we're not accessing it like this because it's not returning a collection item and we're going to say latest comment
03:25
title let's go over give that refresh and what's happened here so let's just have a look and see what is going on here let's just go ahead and dump this out and see what we get here ah okay yeah so oh sorry it doesn't have a body it doesn't have a title it's a
03:41
body that's that's exactly why it wasn't showing okay so there we go it does work i just uh used the wrong attribute so we've got now the latest comment from that article now notice here of course we now have two queries so what we can do to get around this is
03:59
we can go ahead and also include within our with latest comment that will go ahead and eagle load in the latest comment from each one and we end up with only five queries obviously now the more articles we start to iterate through the queries are going
04:15
to remain the same so just make sure you focus on eagle loading as well because otherwise you're going to end up with a huge amount of queries okay so the next thing that we can do here is pretty straightforward let's go over it see the exact opposite of what we've just
04:29
done and this is the oldest comment so let's say oldest comment and you guessed it we're going to say oldest of many and that's what we need to do we just sort of swap this over and there's a little bit of duplication here but it's fine
04:43
it's really really clear what we're doing let's go back over to our web routes again what we would do if we wanted to access this we would eagle load in the oldest comment we might even be showing both and then we would go ahead and we would access oldest
05:00
comment body and then any information about that and there we go we get the oldest comment in there and it doesn't look like I've eagle loaded this in properly or at least I've eagle loaded oh yeah sorry because we're eagle loading both things which is normal so this is still
05:13
not an n plus one problem okay so the last thing that we're going to look at is probably the most fun now we're going to this is going to involve just sort of tweaking the database manually a little bit again but obviously if you're going to transfer
05:26
this knowledge to your own projects you'd go ahead and set this up in migrations but what we're going to show now is on this page we're going to iterate through all the articles or you could just show it on a single article page it doesn't matter we are going to show the article that
05:39
has the most likes or the most you know upvotes or whatever so this is again a really useful thing if you're working with polymorphic relationships that you can do now let's just tidy this up a little bit I'm going to get rid of
05:53
these two here and let's go over to our home page and just make sure we get rid of this so comment with most let's call it likes because that sort of matches the documentation so how do we do this well first of all we're going to modify the
06:08
database schema just manually adding in a likes with an integer let's make that big in unsigned it's fine and let's go and add in some likes so what have we got here we've got an episode so we're going to sort of ignore that one
06:26
let's say comment one has the most likes I'm going to set that to 200 let's set each of these in here and that will do so we would expect to see comment one returned with the most likes and for comment or article two we'd
06:46
expect to see comment six returned with the most likes it's a really simple system but this will give you an idea of how this works so how do we do this so let's go over to our article where we have our list of comments and let's find out another relationship again a little bit
07:03
of duplication but it you know helps to keep these relationships super separate and makes your code a lot less brittle so let's say most liked comment we'll keep it super super simple and we're again going to do a morph one here
07:20
because we are using an of many type so we're going to say comment class commentable now what do we do here well we just use the of many which allows us to provide a sort of custom thing these are just actually wrappers behind this so you can see this
07:39
uses of many behind the scenes using a column to order the created that date basically so now we actually have the chance to go in and provide a custom value for this so we're going to say likes and we're going to say max basically
07:57
so we're going to max up the amount of comments for the likes that we have in terms of the highest one and likes is the column that we just added and of course you can tweak this based on any of the data that you're working with but
08:10
let's go over and see what this looks like once we actually get to output them so we're going to say article most liked comment let's just dump it out for now and there we go so sure enough this one with 200 likes is the most liked comment for that
08:30
article and this is the most liked comment for this other article as well and again it looks like we have two queries here which is going to continue to increase as we go on so of course what we want to do
08:44
is make sure that we're always egoloading and we're going to egoload the most liked comment for each article that we iterate through and let's just have a look here most liked comment there we go so we're down to three
08:58
queries one big one for everything and of course we are now away from that n plus one problem so there are three ways that you can use the of many relationship or modifier which is being used for every single one of these like we've seen
09:12
but these are just sort of convenient wrappers to order the comments you want but all of this stuff combined is super powerful pretty much allows you to output this data these relationship data however you need to

Episode summary

In this episode, we dive deep into the one of many relationship modifier in Laravel, which is a handy tool for grabbing a single item out of a group of related models—think "the latest comment" or "the most liked comment" on an article. It's not its own relationship type but rather a modifier you can use to make your relationships much more focused and efficient.

First, we look at the classic example: how to show just the latest comment for each article, instead of loading up all comments and manually picking the first or last (which is both slow and memory-hungry). We set up a custom relationship using latestOfMany() so you can access article->latestComment just like any other relationship, and discuss the importance of eager loading to avoid the dreaded N+1 query problem.

Next, we flip things around and fetch the oldest comment using oldestOfMany(), showing you how easy it is to swap modifiers and get exactly what you need from your relationships.

Finally, we get a little fancier and fetch the most liked comment by tweaking the database and using ofMany('likes', 'max'), which returns the comment with the highest number of likes for each article. You’ll see how this pattern lets you create clear, focused relationships—and keeps your code readable and maintainable.

Throughout the episode, you’ll see practical code examples, some database tweaks, and tips on eager loading to keep your app speedy. By the end, you’ll be comfortable applying "of many" modifiers to tailor data fetching to your needs!

Episode discussion

No comments, yet. Be the first!