Creating Like and Follow Models0:00
Now we need to be able to track likes, follows and reposts. Let's get started. Let's start by creating the Like model. And just like everything else, we'll create the migration in the factory. Let's also go ahead and let's create the Follow model because now that I think about it, reposting is, well it's not another model, or at least it could be. I don't know if we want to go that approach. I don't know.
well it's not another model, or at least it could be. I don't know if we want to go that approach. I don't know. So for right now, we're just gonna stick with likes and follows. So let's open up the likes migration. Let's also open up the follows migration because even though these are two separate models, they are going to be very similar, at least as far as structure is concerned. So for the likes, you know, what is it like?
Building Likes Migration0:53
as structure is concerned. So for the likes, you know, what is it like? It is a Profile liking a Post. So in many ways it's kind of a pivot table. So we will have a foreign ID for the profile_id, which of course needs to be constrained and let's also cascade on delete. And then we need another foreign ID for the post_id. It also helps if you use correct syntax there.
for the postId. It also helps if you use correct syntax there. So our postId once again needs to be constrained and once again we will cascade on delete, but that's it. Uh, if there's a record for a Profile in Post, then there's a, like if there's not a record, there's not a like there, there's really nothing else column wise or data wise that we need to store here. But we do need some checks for integrity.
or data wise that we need to store here. But we do need some checks for integrity. Like for example, we want to be sure that there aren't any duplicate likes. So profileId with an associated postId needs to be unique. We can also set up an index. Now I think it makes sense to do that on the postId and createdAt. So we will have those there.
Building Follows Migration2:08
and created act. So we will have those there. And then for the most part, that's it. As far as our migration is concerned, our follower migration is going to be very similar. So that we will have a foreign id, which we will call the followerProfileId. And of course this needs to be constrained, but we also need to specify the table here, not posts. We need profiles.
but we also need to specify the table here, not posts. We need profiles. And then we also want to cascade on delete, but then we essentially need the same thing for the following profile. So let's just copy and paste, we'll change that to following profile. id constrained for profiles, cascade on delete, and then our timestamps. But we essentially want to set up the same kind
and then our timestamps. But we essentially want to set up the same kind of restrictions in that it needs to be unique between the followerProfileId and the followingProfileId. We don't want duplicate records for those two exact values. And two, it would be useful to have some indexes. So let's index both. We'll have followerProfileId. Then let's have an index for followingProfileId. And I think that's gonna be it for that migration.
Defining Like and Follow Models3:28
Then let's have an index for following profile id. And I think that's gonna be it for that migration. So we can close that file. Let's open up the Like model as well as the FollowProfile. And we will need to open up several other models. But right now we'll get started with these. Starting with the Like, let's go ahead. Let's add the protected fillable array. And what do we have? We had the profileID.
Let's add the protected fillable array. And what do we have? We had the profile_id and we also had the post_id. And then we just need the methods for the profile, which we can say that this like belongsTo the Profile. So we will simply return this belongsTo and the Profile class. But then we also need essentially the same thing for the Post.
But then we also need essentially the same thing for the Post. And the Post is the same kind of relationship. It belongs to that Post. So we just need to make the necessary changes. And that's gonna get us for the Like model. So we can close that. The Follow model, well, let's go ahead. Let's start with protected $fillable and then the fields where $follower was a follower.
Let's start with protected fillable and then the fields where follower was a follower. I think it was follower, followerProfileId. Then the followingProfileId. And that gets us started there so that then we will need our methods. One for the follower, which we'll just call it follower. And this is a belongsTo relationship. But when it comes to returning, this belongsTo, we of course need to include the Profile class,
But when it comes to returning, this belongsTo, we of course need to include the Profile class, but we also need to use that column, which was follower_profile_id. So that's gonna get us the follower. Then we need the following, which we can just copy and paste. It's going to be a belongsTo as well, except that the column name is following_profile_id. And I think that's going to serve us just fine.
Adding Post and Profile Relationships5:28
that the column name is following profile_id. And I think that's going to serve us just fine. So that then let's open up the Post model because yes, we have the profile, we have the parent for the replies and the replies for the replies. But now we need to know the likes. So we will have a method called likes, which is a hasMany relationship so that we will return this as many Like class. And there are so many other things that we could do,
as many like class. And there are so many other things that we could do, like getting the count of likes and things like that. And I know that we're gonna need that at some point because I remember using that in one of our views. Maybe it was multiple views, but I think for now we'll just leave this as is because we don't need to worry about followers or anything like that with our Post model. We do however need that with our Profile.
or anything like that with our Post model. We do however need that with our Profile. So let's open that up. In which case we will add another method called likes. Once again, it's a hasMany relationship so that we can return. This hasMany and Like class, but then we need our followers and our following. So let's start with the followers, which this is going to be a little bit different because we could say
So let's start with the followers, which this is going to be a little bit different because we could say that this belongs to many, in which case we are gonna have to return. This belongs to many. But then we need to provide a lot of information such as the class. This is a relationship with the Profile class. Then we need to specify, the table follows. Then we need the pivot key, which is following.
Then we need to specify, the table follows. Then we need the pivot key, which is following profileId. 'cause this is for our followers. The followers are following this profile. And then we have the related pivot key, which is followerProfileId. And yeah, that should get our followers, although it does help if you use actual php syntax and make that a method.
although it does help if you use actual php syntax and make that a method. And if we have our followers, then we need who we are following, which once again is a belongsToMany. The Profile class, the follows table. And then we just need to switch up these key values and that should be it. So we now have our users, our profiles, our posts, our likes, and our follows.
Deferring Reposts Feature7:57
So we now have our users, our profiles, our posts, our likes, and our follows. The next thing that we need is our reposts, which I was originally going to include in this episode, but we will wait and look at it in the next episode.
