Creating a Following Feature
To make a personalised home feed, the user must be able to follow a subweddit to recieve its recent posts.
The same thought process for creating a seperate 'comments' table went in to the follow feature of the site, I will create a Follows table containing the a user's id, and the id of the subweddit they follow.
A user follows a subweddit. So, a subweddit can belong to many users. that is our relationship
another relationship which will be handy to know is if a user is following a subweddit or not, it will take in the subweddits id and the user id to check the follow table, and return a true or false.
From here, we could also create a follow and unfollow method and call them seperately, but we would require two forms on the site, thus two buttons. Not very clean, so a more efficient way of creating this functionality would be to create a button, or form, that does both in the form of a toggleFollow method.
This method would check if the user is following the subweddit, if is then it will unfollow() to remove the row from the database, if it isnt then it will save the details in to the database.
How would this display itself on the site? one button, which checks if the user is following the subweddit using if(auth()->user()->following($subweddit->id)
There we have a follow feature! Before we move on to creating a timeline, we can clean up our User model a little by creating a 'trait' for the user to inherit. this trait will be called 'followable' and will contain everything related to following.
in the User.php file:
A timeline is made possible with a new relationship in our User model. A user HAS a timeline, which we can make by plucking (or retrieving) the id's of all the subweddits the user follows. Then, we can create an array of Posts where the subweddit_id is equal the array of id's we made previously. And for that social media flair, all posts show latest first.
Comments
Post a Comment