Coding

Laravel 5.2 PHP Build a social network – Deleting Posts

  • 00:00:00 hi welcome back in the last video we
  • 00:00:04 managed to output our posts and this
  • 00:00:07 video will make sure that we can get rid
  • 00:00:09 of them by deleting them let's start so
  • 00:00:12 the place to start here is in the post
  • 00:00:16 controller and here I will create a new
  • 00:00:20 function at the bottom which we'll call
  • 00:00:25 public function get delete post now this
  • 00:00:34 function will take a parameter we'll
  • 00:00:38 take an argument which I will call post
  • 00:00:42 ID because we need to know the ID of the
  • 00:00:45 post which you want to delete right now
  • 00:00:48 to do this or to actually delete a post
  • 00:00:51 that was very easy to do too
  • 00:00:53 first I will fetch the post and here we
  • 00:00:56 will see another version or another
  • 00:00:59 method of getting posts from our
  • 00:01:02 database in our dashboard function here
  • 00:01:04 we got all the posts now we're going to
  • 00:01:06 get a single post we do this by writing
  • 00:01:09 post and then we're accessing the where
  • 00:01:14 method where we then specify the field
  • 00:01:19 in which you want to search in this case
  • 00:01:22 D ID field and well then the criteria
  • 00:01:26 which is in this case post ID then we
  • 00:01:30 have to call call first queue I'll get
  • 00:01:34 the first element in the resulting array
  • 00:01:37 which will only hold one element but if
  • 00:01:39 we were to call get which is the
  • 00:01:42 alternative we would still get an array
  • 00:01:44 and not a single element which we
  • 00:01:46 couldn't process then and the way we
  • 00:01:48 want to process therefore I'm calling
  • 00:01:50 first as it is only one element anyway
  • 00:01:53 now be sure to know that alternatively
  • 00:01:56 to using where which is the generic way
  • 00:01:59 of creating a wear conditioner or a kind
  • 00:02:02 of SQL statement we could when we're
  • 00:02:05 just looking for a de ID also use find
  • 00:02:08 and unjust post ID and this will
  • 00:02:11 automatically translate into aware
  • 00:02:14 d equals and then whatever we're passing
  • 00:02:16 here method or statement but I like to
  • 00:02:21 use the default way which we have to use
  • 00:02:23 anyway when we're searching for our
  • 00:02:26 criteria now be aware here we're
  • 00:02:30 automatically looking for equality if we
  • 00:02:33 would have a search criteria where we
  • 00:02:36 want to let's say get all posts which
  • 00:02:38 have ID greater than 10 then we would
  • 00:02:41 have a third parameter here in the
  • 00:02:44 middle so the second parameter in this
  • 00:02:47 method call and here we would then
  • 00:02:50 specify as a string well the type of
  • 00:02:54 comparison so greater smaller equal
  • 00:02:57 whatever but equal as to default and
  • 00:03:00 here we're looking for the equal ID cell
  • 00:03:02 we can't just use the two argument
  • 00:03:05 called
  • 00:03:06 dysfunction so this is the way we get
  • 00:03:09 our single post with respective ID and
  • 00:03:11 now if you want to delete it what we
  • 00:03:13 just call post delete and it will be
  • 00:03:17 gone thereafter I want to return around
  • 00:03:21 a redirect to a route and this route
  • 00:03:26 shall be the dashboard route and here
  • 00:03:31 again I also want to provide a message
  • 00:03:35 so I'm just using the width method here
  • 00:03:38 at the end and I will send a message
  • 00:03:43 which says successfully deleted
  • 00:03:48 now remember message here is the
  • 00:03:51 variable which were then using in this
  • 00:03:54 info box at the top of our dashboard
  • 00:03:56 view so let me save this and well let me
  • 00:04:02 reload this page and let's try this hmm
  • 00:04:05 nothing's happening well that's no
  • 00:04:07 wonder we get our logic here but we
  • 00:04:09 haven't hooked it up to the view so we
  • 00:04:12 have to go to our dashboard here and
  • 00:04:14 here where we are outputting all our
  • 00:04:16 posts what I want to do is hook up this
  • 00:04:19 delete route with this delete link here
  • 00:04:21 where to do so I'm going to replace this
  • 00:04:24 hash sign and half of this link
  • 00:04:27 the template expression and if you I
  • 00:04:29 will call a route which I will call post
  • 00:04:32 dot delete now this route doesn't exist
  • 00:04:35 as of now but I will create it in a
  • 00:04:37 second first I want to make sure that I
  • 00:04:40 pass an argument to this route because
  • 00:04:42 remember we're expecting one in our post
  • 00:04:44 controller we're expecting a post ID
  • 00:04:47 therefore I will have to pass this ID I
  • 00:04:51 do this by specifying a second parameter
  • 00:04:53 here in my route method and this will be
  • 00:04:56 an array with well all the arguments all
  • 00:04:59 the variables we want to pass through
  • 00:05:01 this route to this controller action so
  • 00:05:04 I'm going to have only one variable
  • 00:05:06 which will be post ID and again make
  • 00:05:08 sure this name here has to match this
  • 00:05:12 name of the variable you're expecting in
  • 00:05:14 this function and the controller so I'm
  • 00:05:18 passing a post ID and I have to map an
  • 00:05:21 actual value which I pass and this will
  • 00:05:23 just be well ID of the post be currently
  • 00:05:26 at in our loop and I can access this as
  • 00:05:29 a property of our post object by just
  • 00:05:31 writing post and then arrow arrow ID
  • 00:05:35 okay so that's how I'm passing the post
  • 00:05:38 ID now I have to create the route I do
  • 00:05:42 the snow routes file obviously I will do
  • 00:05:45 it here below the create post route and
  • 00:05:48 this will be a get route because we're
  • 00:05:50 accessing it through through a link
  • 00:05:52 through an anchor tag and here we only
  • 00:05:55 can send get requests at least without
  • 00:05:58 JavaScript and to do this here well I
  • 00:06:02 want to call around which will be called
  • 00:06:05 post delete or this will be the URL
  • 00:06:08 we'll see and here I want to use my my
  • 00:06:15 post controller post controller add post
  • 00:06:21 delete post they call this get me post
  • 00:06:27 obviously because I just said it's
  • 00:06:30 supposed get route not a post route and
  • 00:06:33 it should have a name of post delete
  • 00:06:38 that's the name I'm also using in the
  • 00:06:39 dashboard where
  • 00:06:40 looking up my road and this should be a
  • 00:06:43 comma good so this is this and we can
  • 00:06:48 also protect this route with middleware
  • 00:06:52 because this should also only be
  • 00:06:53 accessible when we're locked in and the
  • 00:06:56 same goes for this post creation route
  • 00:06:58 we don't want anyone to be able to let
  • 00:07:01 save through a terminal
  • 00:07:02 create a post which would be possible if
  • 00:07:04 we're not protecting this route now
  • 00:07:07 here's one thing missing we're passing
  • 00:07:08 this idea of the post you want to delete
  • 00:07:11 now that's a get route therefore we have
  • 00:07:14 to specify the ID in our URL right
  • 00:07:17 that's where we are fetching it in our
  • 00:07:19 controller so I'm going to add an
  • 00:07:22 argument and let me rename this I think
  • 00:07:25 it's easier to read if I call this
  • 00:07:27 delete post and then slash and here I
  • 00:07:32 want to specify the post to delete now
  • 00:07:35 to tell arrabal that this will not be
  • 00:07:37 just a normal word in our URL but
  • 00:07:41 instead should be replaced with the
  • 00:07:43 value of the variable we'll use single
  • 00:07:46 curly braces and inside this braces
  • 00:07:48 we're giving it the name of this
  • 00:07:51 parameter post ID and this name
  • 00:07:57 obviously has to be the same like this
  • 00:08:01 one here in our controller so let me
  • 00:08:04 save this and reload this page and now
  • 00:08:07 if I click delete here you see well
  • 00:08:11 successfully deleted and it's gone here
  • 00:08:13 and if I reload in my database
  • 00:08:14 you see we only get three posts now we
  • 00:08:18 get a problem I created one post as
  • 00:08:21 another user our test user here now I'm
  • 00:08:25 able to delete this post too I don't
  • 00:08:28 think that should be possible I think
  • 00:08:29 you should only be able to delete your
  • 00:08:31 own posts we're going to cover how to
  • 00:08:34 protect against this as well as to how
  • 00:08:37 to log out users because that's
  • 00:08:39 something we can't do as of now in the
  • 00:08:41 next video see you there bye