Purchase access to watch this video.

Unlock course for £159

Already purchased access? Login

Updating videos

  1. We're now listing videos for administrators, but it would be nice if
  2. administrators could also
  3. update these videos. Let's work on that. First, let's add a new column for
  4. actions.
  5. Inside this column, we're going to add an inline list with one item. That's
  6. going to be a link
  7. to an edit screen. We'll link to our existing admin videos edit route. We'll
  8. also need to
  9. pass the current videos of priority. If we refresh the page, we'll now see that
  10. we've got an
  11. actions column in the table and edit buttons next to each video. If we click
  12. the edit button for
  13. a video, we'll just get a blank screen because we're actually fleshed out the
  14. controller action
  15. for updating videos. Let's work on that. Open up your video controller class.
  16. And in the edit action,
  17. we're going to return a view. It's going to be admin videos edit. We're going
  18. to pass
  19. the video that is injected via root model binding. Let's create this view.
  20. We'll crop the index, paste that,
  21. update the title, and we'll get rid of the table and pagination links.
  22. We now need to add a form to this page to allow the user to choose the video's
  23. name and description.
  24. When we install Breeze, I created a profile route that includes the forms to
  25. update your profile
  26. and password. So we can grab one of these forms and get the form styling in the
  27. buttons.
  28. Open up your profile edit view. You'll see that these three sections are
  29. actually partials.
  30. So we need to open one of these partials.
  31. And this is the form for the profile information form seen here.
  32. We can grab the contents of this file and paste them into our edit page.
  33. Let's update some of the content.
  34. We can remove this file.
  35. And change the email input to description.
  36. For the name input, set order complete to off. If we lift it, its name, that
  37. will try
  38. an order complete the input's value as a person's name.
  39. We now have two text inputs for name and description. Our other description is
  40. going to hold more
  41. long form content. So let's change this to a text area instead. Create a new
  42. component,
  43. call a text area.blade.php. Open text input.verb.php, copy the contents,
  44. paste it into our new text area component, and replace input with text area. We
  45. also need to
  46. close the tag. And the value needs to be passed in between the tags.
  47. Set it to an empty string as default.
  48. We can also declare the value prop up here. Back in our edit form, we can
  49. change our text input
  50. component to be an instance of our new text area component instead. We can also
  51. remove the type
  52. attribute. Let's also add some rows. We'll set rows to 5 by default so that the
  53. user has plenty
  54. of space to input the description for the video. We now need to make that form
  55. actually do something
  56. when we submit it. If we look at our edit form, we see that it's got action
  57. attribute and we set
  58. the value to the admin videos update root. So let's open our video controller.
  59. And let's start flushing our update method.
  60. I like to follow various conventions when building applications in Laravel.
  61. One is to stick to resource controllers like we have done so far. Another is to
  62. use form requests
  63. for validation. So let's create a new form request for updating videos. In the
  64. terminal,
  65. create a new request. I'm going to put in an admin videos namespace just so it
  66. follows our
  67. controller namespace. Back in the video controller, replace the rejected
  68. request instance with the
  69. instance of our new update video request. Import the class. If we open the
  70. class, we'll see that it has
  71. two methods, authorize and rules. Authorize is actually optional in form
  72. requests. So we can just get
  73. rid of that entirely. The rules method, however, is going to return an array of
  74. rules that we want
  75. to use to validate our request. So we're passing two fields, a name and a
  76. description. We want both
  77. of these fields to be required. We're also expecting string values of both
  78. fields. Let's declare that.
  79. We should also set the maximum length of the name input to be 255 characters.
  80. That's because in the
  81. database, it's the text input and 255 characters is the maximum it can hold.
  82. Description is a text type
  83. column, which can hold much longer values. Back in the controller, we can now
  84. update the video
  85. injected via root model binding with the values we've just validated in the
  86. request.
  87. We can also return back to the edit form. We can also indicate that the video
  88. was updated.
  89. If we refresh our videos listing, then click the edit link next to one of the
  90. videos.
  91. Change the name of the video. I click the save button. We should see a saved
  92. message like we would
  93. if we updated our profile. If we go back to the videos index, we can see that
  94. the name is updated.
  95. In the next video, we'll create a component to toggle the publish and status of
  96. a video,
  97. so we can actually publish and un-publish videos.
Connect GitHub