Updating videos
Transcript was automatically generated and may be inaccurate.
-
–
We're now listing videos for administrators, but it would be nice if -
–
administrators could also -
–
update these videos. Let's work on that. First, let's add a new column for -
–
actions. -
–
Inside this column, we're going to add an inline list with one item. That's -
–
going to be a link -
–
to an edit screen. We'll link to our existing admin videos edit route. We'll -
–
also need to -
–
pass the current videos of priority. If we refresh the page, we'll now see that -
–
we've got an -
–
actions column in the table and edit buttons next to each video. If we click -
–
the edit button for -
–
a video, we'll just get a blank screen because we're actually fleshed out the -
–
controller action -
–
for updating videos. Let's work on that. Open up your video controller class. -
–
And in the edit action, -
–
we're going to return a view. It's going to be admin videos edit. We're going -
–
to pass -
–
the video that is injected via root model binding. Let's create this view. -
–
We'll crop the index, paste that, -
–
update the title, and we'll get rid of the table and pagination links. -
–
We now need to add a form to this page to allow the user to choose the video's -
–
name and description. -
–
When we install Breeze, I created a profile route that includes the forms to -
–
update your profile -
–
and password. So we can grab one of these forms and get the form styling in the -
–
buttons. -
–
Open up your profile edit view. You'll see that these three sections are -
–
actually partials. -
–
So we need to open one of these partials. -
–
And this is the form for the profile information form seen here. -
–
We can grab the contents of this file and paste them into our edit page. -
–
Let's update some of the content. -
–
We can remove this file. -
–
And change the email input to description. -
–
For the name input, set order complete to off. If we lift it, its name, that -
–
will try -
–
an order complete the input's value as a person's name. -
–
We now have two text inputs for name and description. Our other description is -
–
going to hold more -
–
long form content. So let's change this to a text area instead. Create a new -
–
component, -
–
call a text area.blade.php. Open text input.verb.php, copy the contents, -
–
paste it into our new text area component, and replace input with text area. We -
–
also need to -
–
close the tag. And the value needs to be passed in between the tags. -
–
Set it to an empty string as default. -
–
We can also declare the value prop up here. Back in our edit form, we can -
–
change our text input -
–
component to be an instance of our new text area component instead. We can also -
–
remove the type -
–
attribute. Let's also add some rows. We'll set rows to 5 by default so that the -
–
user has plenty -
–
of space to input the description for the video. We now need to make that form -
–
actually do something -
–
when we submit it. If we look at our edit form, we see that it's got action -
–
attribute and we set -
–
the value to the admin videos update root. So let's open our video controller. -
–
And let's start flushing our update method. -
–
I like to follow various conventions when building applications in Laravel. -
–
One is to stick to resource controllers like we have done so far. Another is to -
–
use form requests -
–
for validation. So let's create a new form request for updating videos. In the -
–
terminal, -
–
create a new request. I'm going to put in an admin videos namespace just so it -
–
follows our -
–
controller namespace. Back in the video controller, replace the rejected -
–
request instance with the -
–
instance of our new update video request. Import the class. If we open the -
–
class, we'll see that it has -
–
two methods, authorize and rules. Authorize is actually optional in form -
–
requests. So we can just get -
–
rid of that entirely. The rules method, however, is going to return an array of -
–
rules that we want -
–
to use to validate our request. So we're passing two fields, a name and a -
–
description. We want both -
–
of these fields to be required. We're also expecting string values of both -
–
fields. Let's declare that. -
–
We should also set the maximum length of the name input to be 255 characters. -
–
That's because in the -
–
database, it's the text input and 255 characters is the maximum it can hold. -
–
Description is a text type -
–
column, which can hold much longer values. Back in the controller, we can now -
–
update the video -
–
injected via root model binding with the values we've just validated in the -
–
request. -
–
We can also return back to the edit form. We can also indicate that the video -
–
was updated. -
–
If we refresh our videos listing, then click the edit link next to one of the -
–
videos. -
–
Change the name of the video. I click the save button. We should see a saved -
–
message like we would -
–
if we updated our profile. If we go back to the videos index, we can see that -
–
the name is updated. -
–
In the next video, we'll create a component to toggle the publish and status of -
–
a video, -
–
so we can actually publish and un-publish videos.