This episode is for members only

Sign up to access "Build a Friend System in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
14. Getting distant models through friends

Transcript

00:00
let's add the ability for users to have statuses much like you would have on some kind of social network feed and then we'll go ahead and look at how we can output all of the statuses from all of our friends in the correct order so probably in the order that they were created
00:15
at the top and we will output them on that dashboard page so let's go ahead and make out a model in here and call this status we'll create a migration alongside of that as well and let's go ahead and open that create statuses table migration so this is just going to belong to a user so just a really standard relationship within arafel it's going to have a user id and we
00:37
can constrain that and then we're going to have just a really simple piece of text in here it doesn't really matter what this is with the body so we're going to just manually fill these in in the database assign them to users and then see them output on the dashboard okay so let's go and run php artisan migrate and let's head over to our user model and let's go down to the bottom
01:02
and let's create out a statuses relationship and a user has many statuses so a pretty straightforward standard relationship okay let's create out a couple of statuses over here so i'm going to create a status for mabel and just say meow let's fill in the created at and updated that date as well and we'll also go ahead and add one in for tabby as well and we'll say meow again and we'll
01:32
update the created that to be at the top so this is the latest one because we want to make sure we order these by the latest at the top okay so rather than just go ahead and add tabby as a friend i'm going to go ahead and manually fill this in and say that alex is friends with tabby and this has been accepted so we'll just manually add that to the database so we don't have to go
01:52
through the ui and now we want to see all of these statuses over on the dashboard so there is a laravel relationship that will handle this for us but we're going to go ahead and use a specific package because if your relationships get a little bit more complicated for example if you have already have a pivot table for the statuses or any other items that you want to go ahead and use
02:16
then this package that we're about to pull in is going to work in a much better way than the standard laravel relationships that we get with eloquent so let's go and take a look at that package and that is eloquent has many deep by the same author as the package we've already used to merge in these relationships so this will allow us to do a huge amount of things there's
02:37
lots and lots of functionality here if you have more complicated relationships and you want to get these via these deep relationships you can do which is why this is such a good package to pull in but let's go ahead and look at a really simple way that we can use this to grab the statuses for our friends so we're going to go ahead and pull this package in and we're going to go ahead over
02:58
to our user model and we're going to pull in the has relationships trait which does not come from eloquent so i'm just going to go ahead and re-index here has relationships that's going to come from that package so now what we can do is we can create another relationship out in here statuses of or from friends something like that of course feel free to name this however you want now the
03:29
way that we're going to do this is using an existing relationship we already have a statuses relationship so we can use this has many deep from relations we can pass in the relationship for that model and then go ahead and grab the relationship like this so let's go ahead and use this has many deep from relations and see how this works i'm actually going to go ahead and call this method
03:52
friends statuses i think that makes a little bit more sense so let's say this has many deep from relations the relationship that we want to grab this via other friends so friends we pass in here and then we basically just want to new up a user model and then we want to pass in the actual relationship itself now this won't grab the statuses so this won't perform a query the
04:18
statuses relationship returns to us a has many relationship type so this will basically allow this package to work out how it needs to pull this in and who via which is our friends so our friend statuses are there let's go ahead and create our dashboard controller so we can make this a little bit better because by default this is just a root closure so i'm going to create a dashboard
04:39
controller here and let's head over to our roots switch this up really quickly so i'm going to go ahead and pull this closure out of here and put that dashboard controller in there and we'll leave the middleware in here and all that kind of stuff let's head over to our dashboard controller let's create an invoke magic method and let's return that dashboard view so that shouldn't change
05:02
anything whatsoever over on here great okay let's grab the request in here just so we can grab that user and let's just die dump on this first of all so let's say request user and statuses or what do we call this friends statuses great so we should now have a collection of two items which we do so we've got a status here from tabby and a status here from mabel because they are our friends now
05:31
because we are friends with both of them they should be able to see my statuses as well because they are friends with me so we'll test that out in just a second let's first of all just pass this down and really roughly output these to the page so let's just go ahead and say statuses and there we go and over on dashboard.blade.php let's iterate through these and see them so for each
05:57
statuses as status and that for each and let's just dump these out really really roughly so we'll grab the status user in fact we don't actually have that relationship in there so let's go ahead and create this out this belongs to a user so we just reverse that relationship status user name and then the status body
06:30
there we go and that should be enough to see that out here now because we're using this package and it's going to pretty much work with some exceptions like we would normally define a relationship out what we can do is still order by stuff in here so we can either manually order by the creator that day or we could use the latest helper here to pull the latest at the top which we know is tabbies
06:53
because we created that after we created Mables in the database so now that we've got them two in there now if we head over to Mable's account we should technically over on the dashboard see any of Alex's statuses because we're both friends and these are now public so let's go over and create another status in here from my account and just say hello and save that out and if we
07:23
head over to Mable's account there we go so now anyone who is friends with anyone will see all of them statuses on the dashboard before we go one really important thing that we should know is that we need to keep an eye on our query count so let's just go ahead and pull in Laravel debug bar so we can keep an eye on these so let's go over to our terminal paste this in and if we come over
07:44
to a user's account that has two items here and we have a look at our query count you can see we just pull this up here we are selecting these two users from here which is not great we basically have an n plus one problem here so what we want to do is make sure we eager load these so over on the dashboard over on dashboard controller we want to make sure that for the friends statuses
08:08
we eager load in the friends so to do this all we need to do is go ahead and use get instead and then in between here say with and user so that will eager load from the status here the user for each of them and then we won't need to look them up over and over again and you can see that that's changed that query to select both in two and three rather than execute a query for every single
08:31
status if you had a hundred friends and you saw a hundred statuses from a hundred different friends you would end up with a hundred queries to look up each user for every iteration which is not a good idea so really really important while you're developing anything like this pull in something like laravel debug bar to keep an eye on your query count
14 episodes1 hr 16 mins

Overview

Everything you need to implement a friend system in Laravel, built in a simple UI so you can integrate it into your existing apps.

We'll start with the basic friend relationships, then add in more advanced relations to make things future-proof. Want to easily get all friend's statuses on a timeline? No problem.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!