Coding

Laravel 5.2 PHP Build a social network – Likes & Dislikes (2/3 – Finishing the Backend)

  • 00:00:01 hi welcome back so we started with the
  • 00:00:05 JavaScript implementation in the last
  • 00:00:06 video and we also set up the tables we
  • 00:00:10 need enter relations so now it's time to
  • 00:00:14 work a little bit more on the back end
  • 00:00:16 so first I will create a route we set up
  • 00:00:20 at the end of the last video and this
  • 00:00:21 will be a post route leading to slash
  • 00:00:24 like and it will use a controller action
  • 00:00:28 even though we haven't created that yet
  • 00:00:31 so we will have post controller here and
  • 00:00:36 then post like post let's say and then
  • 00:00:42 here as a name for this route you will
  • 00:00:44 have like and his name is important
  • 00:00:46 because in the dashboard few who are
  • 00:00:49 already using this name here so they
  • 00:00:52 should match the name you just created
  • 00:00:54 here in the routes file this is like
  • 00:00:57 road now in a post controller let's
  • 00:01:00 create this new function public function
  • 00:01:03 post like post this method here will
  • 00:01:10 take or will get a request object and
  • 00:01:13 check that sort of that can access the
  • 00:01:16 parameters or the data passed with this
  • 00:01:18 HX request and what I want to do here is
  • 00:01:24 I want to retrieve the post ID so this
  • 00:01:29 is request post ID if you remember
  • 00:01:33 remember this from my JavaScript file
  • 00:01:36 setting this post ID here and I'm also
  • 00:01:43 sending this is like data so this will
  • 00:01:48 be the second thing I extract here just
  • 00:01:51 check if it is is like if it well it's a
  • 00:01:56 liking actual we're doing he here or if
  • 00:01:59 we're disliking something there's one
  • 00:02:02 thing I want to do or what we have to be
  • 00:02:04 careful what we're getting passed here
  • 00:02:08 will be interpreted as a string so does
  • 00:02:12 it's not a boolean
  • 00:02:13 so what I will he do here is I will
  • 00:02:16 check if we get true if what we get
  • 00:02:19 submitted is true as a string and then I
  • 00:02:23 will well set it to true our wise to
  • 00:02:28 false so kind of Quan word it now of
  • 00:02:35 course as before I can't get rid of this
  • 00:02:38 and just save this check here but it's
  • 00:02:42 important for the rest work correctly as
  • 00:02:45 you will then see the next thing is I
  • 00:02:48 will have two different actions whether
  • 00:02:52 I'm updating and entering the database
  • 00:02:55 or if I'm adding a new one because what
  • 00:02:58 are the cases we could have I already
  • 00:03:00 explained them a bit at the beginning of
  • 00:03:02 the last view now it's time to dive into
  • 00:03:04 that deeper we have this table where we
  • 00:03:09 have post ID user ID and the third
  • 00:03:11 column which saves is if it is a like'
  • 00:03:14 or dislike' on the dashboard we have two
  • 00:03:17 like and dislike button case one is I
  • 00:03:22 neither like nor dislike this post like
  • 00:03:26 in this case both buttons would say like
  • 00:03:28 or dislike the database nothing is safe
  • 00:03:31 because I haven't published my opinion
  • 00:03:34 regarding this post yet next option is I
  • 00:03:39 liked his posts I click on like now in a
  • 00:03:42 database and you row will be created in
  • 00:03:45 the in in the table in the like table
  • 00:03:49 where I saved it I the authenticated
  • 00:03:53 user like this post so the like column
  • 00:03:57 will be set to one to true for this row
  • 00:04:01 so it's the second case
  • 00:04:04 now I do already like this post to this
  • 00:04:08 text put something and say something
  • 00:04:10 like you liked his post or so now I
  • 00:04:13 could click on this you like this post
  • 00:04:16 link to undo my liking of this post but
  • 00:04:23 not to dislike it which again
  • 00:04:26 something different here as this is kind
  • 00:04:29 of a negative expression or I'm really
  • 00:04:33 saying I really don't like it instead of
  • 00:04:35 saying I I just don't care so I click on
  • 00:04:39 like after I already liked this post and
  • 00:04:42 I want to undo this liking so it's the
  • 00:04:45 third case and so now what we do is we
  • 00:04:49 have a row in the database table for
  • 00:04:53 this user and post and currently it's
  • 00:04:55 set to one now when I click on like
  • 00:04:58 again I will just delete this row
  • 00:05:00 because I don't want to turn it into a
  • 00:05:02 dislike I will just get rid of it so
  • 00:05:04 that I'm back to case one where I
  • 00:05:07 haven't expressed any opinion on this
  • 00:05:09 post yet okay next possibility is I
  • 00:05:13 already liked this post but now I decide
  • 00:05:16 no not only do I not care so I'm not
  • 00:05:19 undoing the like I will turn it into a
  • 00:05:23 dislike instead because now I really
  • 00:05:25 don't like this post anymore therefore I
  • 00:05:29 don't click on you like this post to
  • 00:05:32 undo the liking but I directly click on
  • 00:05:35 dislike so when doing this I could
  • 00:05:40 delete the room and enter a new one with
  • 00:05:44 the last column set to zero instead of
  • 00:05:46 one but why would I do it the easier way
  • 00:05:48 is to just update the entry so that I
  • 00:05:52 change the one the true in the like
  • 00:05:55 column to zero so that now it is a
  • 00:05:58 dislike so that are the four cases I
  • 00:06:01 have and of course mice words are for
  • 00:06:05 disliking but that's basically the same
  • 00:06:07 so this is what I will have to somehow
  • 00:06:13 cover in my database or in the
  • 00:06:15 background
  • 00:06:17 it sounds may be a bit more complicated
  • 00:06:20 than it is but I will start by adding a
  • 00:06:22 update variable which is set to false by
  • 00:06:26 default this variable is only there for
  • 00:06:29 me to store that after checking if the
  • 00:06:34 user already likes this post and so on
  • 00:06:36 and if he now wants to dislike it
  • 00:06:38 instead of undo
  • 00:06:39 the liking that I can store it if what I
  • 00:06:42 need to do in the end is updated update
  • 00:06:44 this entry or a save a new one and I
  • 00:06:47 want to keep track of that with this
  • 00:06:49 variable next thing is I will find this
  • 00:06:52 post we're talking off supposed and then
  • 00:06:55 I will use post now finds a shortcut
  • 00:06:58 here to find it post find the post idea
  • 00:07:02 extracted and now we don't need first or
  • 00:07:05 get after dead just semicolon is enough
  • 00:07:09 this will automatically or already
  • 00:07:10 retrieve the post for us and then I
  • 00:07:14 check if well if I had found something
  • 00:07:17 if they got the post and I will then
  • 00:07:21 return also I will just exit yeah it
  • 00:07:25 would be better to return some jason
  • 00:07:27 with an error message but here I will
  • 00:07:29 just well just leave that method and
  • 00:07:32 nothing happens because I did not find
  • 00:07:35 the post or whatever happens something
  • 00:07:37 wrong something's wrong next thing is I
  • 00:07:40 will retrieve the user this is easy just
  • 00:07:42 off user again because well I'm always
  • 00:07:45 doing this from the view of the logged
  • 00:07:47 in user next thing is I want to well
  • 00:07:53 check if I already liked this post so if
  • 00:07:59 there already is an entry regarding this
  • 00:08:02 post
  • 00:08:03 oh it just called like and what I do is
  • 00:08:06 I take currently logged in user now used
  • 00:08:11 likes relation we set up to get all the
  • 00:08:14 likes this user head and then I want to
  • 00:08:19 see if from the likes of this user or in
  • 00:08:24 all the likes this user did in the past
  • 00:08:27 I can find one where the post ID off
  • 00:08:33 that like matches the post ID of the
  • 00:08:37 post I'm now trying to like or dislike
  • 00:08:40 and here I get the first element there
  • 00:08:44 should be only one returned but here I
  • 00:08:46 have to specify specify first to not get
  • 00:08:49 a query boater object but
  • 00:08:52 collection or in this case individual
  • 00:08:54 element so now I will check if I did
  • 00:08:59 find something if I did not find
  • 00:09:01 anything that means I haven't taken any
  • 00:09:04 liking action on this post yet I never
  • 00:09:07 liked or disliked it or at least maybe I
  • 00:09:10 undid it but right now there is no entry
  • 00:09:13 saved regarding that user and that post
  • 00:09:16 but if I do find something so now it's
  • 00:09:20 the case where I already have an entry
  • 00:09:22 regarding this user and post if I do so
  • 00:09:25 find something I want Jack if I'm
  • 00:09:29 currently liking or disliking it so now
  • 00:09:34 I already got this is like variable so I
  • 00:09:37 will call this one let's say already
  • 00:09:41 like there might be a better name but
  • 00:09:45 just to make it clear that it is not the
  • 00:09:48 same like this where we're just
  • 00:09:49 retrieving the data we're getting passed
  • 00:09:51 through this function but here we're
  • 00:09:53 checking if we currently already liked
  • 00:09:56 or disliked this post so what I will do
  • 00:09:59 is I will take the like I retrieved and
  • 00:10:02 access the like property or column
  • 00:10:06 remember just a name we set up here this
  • 00:10:09 column has the name like I'm now
  • 00:10:10 accessing this column here to get if
  • 00:10:13 it's currently alike or a dislike so if
  • 00:10:18 this returns true we already like if it
  • 00:10:23 returns false we already dislike it so
  • 00:10:26 to say I hope this is clear what we're
  • 00:10:28 doing here even way I can send updates
  • 00:10:33 true now because we already gather entry
  • 00:10:35 and we're going to change it the last
  • 00:10:40 question is how are we going to change
  • 00:10:43 it if we're currently liking it and we
  • 00:10:47 again clicked on the like button in the
  • 00:10:50 dashboard we want to undo this liking
  • 00:10:53 ever currently liking it and we clicked
  • 00:10:57 on this like we don't want to undo it we
  • 00:11:00 don't want to delete that entry but we
  • 00:11:02 want you change the light column
  • 00:11:05 zero to dislike now so in this case what
  • 00:11:09 I will do here is I will say if already
  • 00:11:14 like equals
  • 00:11:17 is like so what we passed into this
  • 00:11:22 request in this case I know I already
  • 00:11:26 for example like the supposed and I
  • 00:11:28 clicked on the like button again so now
  • 00:11:30 I only want you undo it I don't want to
  • 00:11:32 change it into a dislike so in this case
  • 00:11:35 I will just delete this like from the
  • 00:11:37 database and then I will return all
  • 00:11:40 about just exit because everything's
  • 00:11:42 done I just want to delete it i undid it
  • 00:11:45 otherwise i will well continue basically
  • 00:11:50 here so let's just write the out
  • 00:11:53 statement for the question if we did
  • 00:11:56 already find a like now you might wonder
  • 00:11:58 whereas the out statement here if I'm
  • 00:12:01 disliking it I'm writing this outside of
  • 00:12:05 this if block here because by returning
  • 00:12:08 now I'm exciting anyway if I'm finished
  • 00:12:11 if I just in here to undo it and
  • 00:12:13 otherwise I can just continue after this
  • 00:12:17 if block because there will be some code
  • 00:12:19 which is now relevant in the else case
  • 00:12:22 of this F statement and D general Al's
  • 00:12:27 case or next steps when creating a new
  • 00:12:30 like but you will see this in a second
  • 00:12:32 and then it should be more clearly so
  • 00:12:34 here I'm checking if I already do have a
  • 00:12:37 like entry for this post user now in the
  • 00:12:40 case I don't I know that I will create a
  • 00:12:43 new like because I don't have one yet
  • 00:12:46 that's all here make sure you add does
  • 00:12:51 use app like import here at the top to
  • 00:12:54 be able to actually use the like here in
  • 00:13:00 this file so otherwise this would result
  • 00:13:03 in an error so now I'm creating a new
  • 00:13:05 like but either way if I'm creating a
  • 00:13:08 new like or using the already created
  • 00:13:11 one and this is what a check was just
  • 00:13:12 talking about now we're continuing
  • 00:13:14 outside of this F statement we
  • 00:13:16 cuz now we're on the same page again I
  • 00:13:18 want you edit this like so in all cases
  • 00:13:23 I will set like column now she would
  • 00:13:27 ever get passed into this router into
  • 00:13:31 this method here so is like which you
  • 00:13:34 were passing for the HX call then I also
  • 00:13:38 will set the user ID equal to user ID so
  • 00:13:45 I currently locked in a user here I'm
  • 00:13:48 accessing his ID or her ID and then I
  • 00:13:52 have post ID and well I already got this
  • 00:13:57 as a variable right here I could of
  • 00:14:03 course also just access post ID because
  • 00:14:08 I retrieved the post anyway here so now
  • 00:14:15 the like is configured either as a new
  • 00:14:19 like if we didn't already have it or we
  • 00:14:21 just changed it if we did already have
  • 00:14:24 an entry but now it's important we
  • 00:14:27 either have to save it if it is a new
  • 00:14:29 one or update it if it already existed
  • 00:14:32 now here is where my update variable is
  • 00:14:37 important remember I said it to true if
  • 00:14:42 you found a like entry so if you already
  • 00:14:44 had one
  • 00:14:44 otherwise it is false by default so if
  • 00:14:47 he already had an entry well all we do
  • 00:14:51 is we update the newly configured like
  • 00:14:55 otherwise I will save it to create a new
  • 00:15:00 row a new entry and then I will again
  • 00:15:04 return null again it would be better to
  • 00:15:06 return some kind of message but it's
  • 00:15:08 fine like this here so now this is my
  • 00:15:12 Ajax request or how I handle it and this
  • 00:15:18 should really work now let me reload
  • 00:15:21 this page
  • 00:15:22 and let me click like here and then I
  • 00:15:30 will open up when ii will open up my
  • 00:15:36 database here it is
  • 00:15:38 and then the likes table you can see i
  • 00:15:41 get a user ID of 1 which will i only got
  • 00:15:45 one user but which is this one and a
  • 00:15:48 post idea of 2 which is supposed with
  • 00:15:51 the body and never post its correct i
  • 00:15:53 clicked on this button I also have a
  • 00:15:56 like state or value of 1 which is true
  • 00:16:01 because I clicked on like if I click on
  • 00:16:04 like again and reload you can see the
  • 00:16:08 entry was deleted because I undid it if
  • 00:16:11 I click on dislike and reload you can
  • 00:16:15 see I got the same entry as before but
  • 00:16:17 now with a value of 0 for like because
  • 00:16:20 it's a dislike if I click on this like
  • 00:16:23 again I'm undoing it but now it's the
  • 00:16:25 interesting part if I click on like I
  • 00:16:27 get a new like entry if I now click on
  • 00:16:30 dislike this should not disappear but
  • 00:16:33 instead only this year should change to
  • 00:16:36 0 and that's exactly what happened also
  • 00:16:39 maybe you recognize that the ID of this
  • 00:16:41 entry didn't change because we did not
  • 00:16:43 delete it and create a new one which is
  • 00:16:45 updated it
  • 00:16:46 that's exactly behavior we wanted now
  • 00:16:50 the last thing is to update the Dom once
  • 00:16:54 were done with well this part here and
  • 00:16:58 we will see this in the next video bye