Coding

Laravel 5.2 PHP Build a social network – Updating DB & View

  • 00:00:01 welcome back now we're finally on the
  • 00:00:03 way to finishing this edit post section
  • 00:00:05 in this video we'll take care that the
  • 00:00:08 whole added post thing is working once
  • 00:00:10 we're finished here so we're at the
  • 00:00:13 point we close this console where we
  • 00:00:15 were passing the post content the body
  • 00:00:17 here and the ID to the router now the
  • 00:00:20 only thing we have left to do is to
  • 00:00:22 actually create a controller function
  • 00:00:24 which will add at the post in a database
  • 00:00:27 so go to my controllers and it was
  • 00:00:30 clearly a shell for a boost controller
  • 00:00:32 so in here I want to add a function here
  • 00:00:36 public function function post edit post
  • 00:00:42 and this again will request and then
  • 00:00:50 inside this function I first want to
  • 00:00:54 validate this request because suppose
  • 00:00:56 could be empty this validate request and
  • 00:01:05 now let's create a rules and the only
  • 00:01:08 rule I have is that the body is required
  • 00:01:12 and you might ask okay it's a it's an HX
  • 00:01:16 request what happens if validation fails
  • 00:01:18 well in this case we will also return
  • 00:01:21 will return it Jason or check with well
  • 00:01:23 the errors regarding to this fail what
  • 00:01:26 we're living this applications will not
  • 00:01:28 have these errors weren't out putting
  • 00:01:30 them at least for a moment but we won't
  • 00:01:33 continue we won't we won't update the UI
  • 00:01:36 so the user will see that the changes
  • 00:01:38 have not been written to the database
  • 00:01:40 if you pass validation what I don't want
  • 00:01:42 to do this I want to get my post and we
  • 00:01:45 have to post ID passed into this
  • 00:01:47 function post will be just getting it
  • 00:01:52 through the post eloquent model here and
  • 00:01:57 we can just find the request post ID
  • 00:02:03 we're passing opposed to being finds
  • 00:02:05 just a handy function to use our post
  • 00:02:07 model which is directly mapped to the
  • 00:02:10 database who element too well to find
  • 00:02:13 this ID were passing into here
  • 00:02:16 we gotta post Europe and now what we
  • 00:02:20 want to do is edit this post so for us
  • 00:02:22 all just said the body of this post
  • 00:02:24 equal to the new body who passed in here
  • 00:02:27 and this is all I have to do right
  • 00:02:29 that's the only thing we can add it so
  • 00:02:31 they're after
  • 00:02:32 I'm calling post update not safe we're
  • 00:02:35 not saving it as a new post were
  • 00:02:36 updating the existing post very
  • 00:02:38 important after this I can return your
  • 00:02:42 response which Jason I'm here I want to
  • 00:02:53 really hurry a Charles would object
  • 00:02:58 so yeah I'm pulling this up very well
  • 00:03:01 we'll just send a message post edited
  • 00:03:08 something like this so this is the
  • 00:03:12 function we have you're now in our route
  • 00:03:14 Red Hook this function up there rebel
  • 00:03:16 get rid of all this array here say which
  • 00:03:22 actually should be useful controllers
  • 00:03:24 few so obviously we're in there whose
  • 00:03:26 control here at coast added hose and
  • 00:03:30 this should have a name off we'll add it
  • 00:03:32 again so we've all seen that before now
  • 00:03:36 we're passing this to a real functioning
  • 00:03:38 controller this will validate it and
  • 00:03:40 invalidation succeeds it will edit it if
  • 00:03:43 it's not succeed succeed while it won't
  • 00:03:45 edited so to actually see something let
  • 00:03:53 me go here and now I'm just outputting a
  • 00:03:56 string of five version of whatever we're
  • 00:03:59 getting back so that we can see what
  • 00:04:00 happens if we well don't pass validation
  • 00:04:05 so let me reload here and move my dev
  • 00:04:09 tools like edit and save changes you'll
  • 00:04:13 see we get this message post edited now
  • 00:04:16 let's see a new posts as many as here
  • 00:04:20 and it was edited we don't see it here
  • 00:04:25 we go
  • 00:04:26 for a moment here but now I see was
  • 00:04:29 successfully edited in the database
  • 00:04:31 otherwise it would not appear now
  • 00:04:32 because we're fetching the stay or data
  • 00:04:34 from the database
  • 00:04:35 each time we refreshing was paid right
  • 00:04:37 so this is cool this Maurice know what
  • 00:04:40 happens if this is an empty field when
  • 00:04:44 we get an error here we got the 422
  • 00:04:48 error message and so now if we reload we
  • 00:04:52 see nothing happened it well did not
  • 00:04:55 pass and that was all we're doing for
  • 00:04:57 now so the last thing is when we added
  • 00:04:59 this we want to close this model and
  • 00:05:02 instantly update our post here right
  • 00:05:05 this is how it should feel
  • 00:05:06 now we don't want to reload our page and
  • 00:05:09 therefore he changed the text on the
  • 00:05:12 currently active page when we
  • 00:05:14 successfully updated our data in the in
  • 00:05:17 the database I will you know do this in
  • 00:05:20 this dumb function here where I know it
  • 00:05:23 will only get if we have successfully
  • 00:05:24 passed validation now I might get there
  • 00:05:27 even though there is an error in the
  • 00:05:29 database but that's very unlikely in our
  • 00:05:31 case here and it would of course be easy
  • 00:05:33 to implement some functionality to check
  • 00:05:36 if we're actually successful or not so
  • 00:05:39 in this case we're going to say when
  • 00:05:40 we're inside the stomach whole bag we're
  • 00:05:43 good to go
  • 00:05:43 and data has been updated in this case I
  • 00:05:47 then want you change the post body here
  • 00:05:51 this text here basically to the new text
  • 00:05:55 to do this I will create a new variable
  • 00:05:57 here at the top okay I call it post body
  • 00:06:00 element so to knowledge beginning and I
  • 00:06:03 will initialize it here post body
  • 00:06:06 element should just be this year so
  • 00:06:10 about the text but the pure element and
  • 00:06:14 I will obviously then use post body
  • 00:06:17 element here and then down here I can
  • 00:06:20 use jQuery pass this post body element
  • 00:06:22 variable into it and I call the text
  • 00:06:25 function to output some text now the
  • 00:06:29 text I want to output should be sent
  • 00:06:31 back from the post controller now for
  • 00:06:35 you
  • 00:06:35 yes I will edit this message here no
  • 00:06:39 longer same thing pick a message but
  • 00:06:40 instead let's say we're sending back a
  • 00:06:42 new body property which is set equal to
  • 00:06:46 post body so that's definitely the post
  • 00:06:49 body were writing into database and
  • 00:06:51 therefore I'm sending it back to the
  • 00:06:54 Chawla script here obviously you could
  • 00:06:56 also implement other ways of handling
  • 00:06:59 this you could update it as soon as you
  • 00:07:01 click this button here for example
  • 00:07:03 because you might say hey I'm sure it
  • 00:07:06 works and we do this kind of eager or
  • 00:07:09 optimistic
  • 00:07:10 updating else so you will see doesn't
  • 00:07:12 matter what publications are not because
  • 00:07:14 normally our database access would
  • 00:07:16 obviously take some time and maybe we
  • 00:07:18 don't want to use to wait that long so
  • 00:07:20 this would be pro q better idea but yeah
  • 00:07:24 this will be very easy to implement you
  • 00:07:25 which just updated right away once it
  • 00:07:27 clicks the Save button
  • 00:07:28 we're doing it where we're getting back
  • 00:07:30 our data now in this case I will access
  • 00:07:35 my message element your new body was
  • 00:07:39 like all right new body property which
  • 00:07:43 will be exactly the text we stored in a
  • 00:07:46 database as the new body once it's done
  • 00:07:48 let's try it cuz I reload my page click
  • 00:07:52 Edit and you posts enter something yeah
  • 00:07:55 as you see it updated all you fingers I
  • 00:07:58 want to close the model I can do this
  • 00:08:00 here in the done functions well let's
  • 00:08:05 say I was just target my edit model and
  • 00:08:12 here I can call modal him know smooth
  • 00:08:17 hide so let's try if this work we should
  • 00:08:26 here safe and smooth closes poses
  • 00:08:31 updated here and also as you can see we
  • 00:08:34 reload this page in the database so this
  • 00:08:37 is we can edit our post was quite a lot
  • 00:08:39 of work now can and then delete them
  • 00:08:41 also
  • 00:08:42 only edited leader all posts oh that's
  • 00:08:44 one thing we should edit maybe here a
  • 00:08:47 post cooler this check we have here when
  • 00:08:50 deleting posts we should also implement
  • 00:08:52 implemented here or were editing post to
  • 00:08:55 check if the current user is the user
  • 00:08:58 who created this post very important
  • 00:09:01 thing so now we're good to go
  • 00:09:03 everything has been set here see in the
  • 00:09:05 next videos bye