00:00
(upbeat music)
00:02
So at this point within our admin dashboard,
00:06
we can create movies, edit them,
00:09
add, remove cast and crew members,
00:11
but we cannot delete them.
00:12
So that's the last thing here that we need to take care of.
00:15
Let's go ahead and add a little delete button
00:17
right down here that will allow us
00:18
to delete a movie when clicked.
00:20
So let's hide our browser back away.
00:22
For this, we're gonna want to jump into our
00:24
pages admin movies index page,
00:27
and we'll add this into our actions TD,
00:30
which is the last TD in the table for each row.
00:33
We can add this underneath our edit
00:36
and we'll add it in as a form method,
00:39
post action equals route admin movies.
00:43
And the route name here should be destroy.
00:46
We need to pass in the ID as the route parameter,
00:48
which would be our movie.id.
00:50
And we need to signal this as a delete HTTP operation.
00:55
So we can add in a third argument with QS
00:59
and an underscore method to use HTTP spoofing
01:03
to assign this as delete.
01:05
All right, go ahead and add in a couple of spaces there
01:08
and end that element.
01:10
I'm gonna go ahead and break those down
01:11
on the separate lines as our action URL generation
01:14
there is a little long.
01:15
All right, so within this form,
01:16
let's add in a button type, submit and class.
01:19
We want to be text read, maybe 500,
01:23
so that it's noted that this is a dangerous operation
01:27
and we can give the text for that as delete.
01:29
This route should already exist,
01:30
but we can go ahead and double check that.
01:32
I believe we did assign that as a resource.
01:34
So we'll hit command P, type in routes
01:36
and jump into our start routes file.
01:38
Believe our admin section is all the way down at the bottom.
01:41
So we'll scroll on down here.
01:42
And sure enough, there is our router resource movies
01:45
with our admin movies controller as a handler.
01:48
So the route is defined there.
01:49
We can jump into our admin movies controller.
01:52
And if we scroll on down,
01:53
we created this controller as a resource.
01:55
So we should already have a destroy method right here.
01:59
So cool.
02:00
We have the params,
02:01
but we're also going to want the response
02:03
so that we can return and redirect right back.
02:05
And the first thing that we're gonna wanna do
02:07
is query the movie so that we can grab an instance of it.
02:09
So const movie equals await movie.findOrFailParams.id.
02:14
To actually delete this movie out,
02:18
we can do await movie.delete,
02:20
which will delete it out of the database.
02:22
And then we can return response,
02:25
redirect right on back to the admin movies index page.
02:29
Cool.
02:30
So in terms of our movie delete,
02:32
what is this going to do?
02:33
Well, it's going to delete that record out of the database,
02:36
as mentioned earlier.
02:37
If we come over here and we jump into our movie model,
02:40
so right here,
02:41
and we scroll up to the top
02:44
and let's run through what we have.
02:45
So we have a status ID, writer ID, and director ID.
02:49
These relationships are all belongs to relationships.
02:52
They belong to the movie record.
02:53
So whenever we delete the movie record out of the database,
02:56
those relationships will automatically be deleted as well,
03:00
as they were strictly housed on that record itself
03:02
for the movie.
03:03
In terms of our other relationships,
03:06
which if we keep scrolling down here are for our watch lists
03:11
as well as our crew and cast members,
03:13
those are not automatically deleted
03:15
unless we explicitly told them to delete on Cascade
03:19
within our migration.
03:20
Remember we covered that earlier.
03:22
So let's go through and see which of these relationships
03:25
we applied that to.
03:26
So let's go into our database.
03:27
We have our watch list, crew members,
03:29
and cast members relationships that we're looking at here.
03:32
So let's dive into the migrations.
03:33
I'm going to expand this out
03:34
so that we can see the full names
03:36
and we can scroll down a little bit to see them all.
03:37
These are our crew and cast member pivot tables.
03:40
So if we go into here and we take a look at the integer
03:43
in charge of the relationship on this pivot table,
03:46
you can see this on delete Cascade.
03:48
So whenever we delete out the movie,
03:50
this pivot table record should be automatically deleted
03:52
because we've told it to delete on Cascade
03:55
with the relationship itself.
03:56
So for our crew movies,
03:58
the deletion should happen automatically
04:00
for that relationship.
04:01
So we shouldn't need to worry about it.
04:02
If we take a look at our cast movies,
04:04
this has the exact same thing going on.
04:06
So for our movie ID, it's got on delete Cascade,
04:09
which when we delete out the movie,
04:11
the deletion will cascade through for any pivot table
04:14
records matching that movie's ID.
04:16
So those will automatically be deleted there as well.
04:18
As for our watch list table,
04:20
it looks like we have the on delete Cascade here
04:23
as well for our movies.
04:24
So we don't need to worry about it there either,
04:26
but what if we did?
04:28
So let's say for example,
04:29
we needed to worry about deleting our watch lists
04:32
in addition to our movie.
04:33
Let's say that we didn't have that on Cascade delete there.
04:35
So it didn't happen automatically.
04:36
What we would want to do is also query the watch lists.
04:39
So const watch lists equals movie,
04:42
and we can reach through the relationship.
04:44
So related watch list.
04:46
At this point, we don't care about the user
04:47
because the movie would no longer exist.
04:49
So we need to automatically remove it
04:51
regardless of the user.
04:52
So we can just do query to grab all of them.
04:55
With each of those watch lists,
04:56
we would then need to delete them
04:57
before we actually delete the movie.
04:59
Otherwise we're gonna get a foreign key constraint error
05:01
as there is a foreign key
05:03
between the watch list and the movie,
05:05
making sure that the movie exists
05:06
for the watch list record.
05:08
So before the movie deletion,
05:09
we would need to,
05:10
with the way that we've queried them here,
05:12
loop through and delete them.
05:13
So we could do for let watch list of watch lists,
05:18
and then do await watch list dot delete.
05:21
Nope, that's a promise.
05:22
We need to also await the query results there.
05:24
Okay.
05:25
Which will loop over each watch list
05:27
that we have individually one by one,
05:29
deleting it out.
05:30
Which would not be the most performant approach
05:32
to this particular deletion.
05:33
Instead, what we'd probably want to do
05:36
is use the actual query builder
05:37
to delete it out altogether.
05:39
So we don't need to query the watch list at all,
05:41
but instead we could do query dot,
05:43
and there is a delete method
05:45
that will delete any matched watch lists
05:47
that we've queried here
05:49
out of the database automatically for us.
05:51
And then once we have those watch lists deleted,
05:53
we're a-okay to then remove the movie
05:56
as any potential foreign key matches
05:57
have now been removed from the watch list.
05:59
Again, we have on delete cascade
06:01
for all of these relationships.
06:03
So we don't need to worry about this at all.
06:05
The deletion will happen on cascade automatically for us
06:08
whenever we delete out the actual movie record.
06:10
So we can omit that altogether,
06:12
and we should be able to just leave things
06:14
how we have it right here.
06:15
And we shouldn't need to manually delete those
06:17
as many and many to many relationships
06:19
that our movies have.
06:20
So with that applied,
06:21
let's go ahead and jump back into our browser
06:23
and give this a go.
06:24
So we have our new delete button right down here.
06:26
Let's try to delete awesome movie number three,
06:29
which should also remove the writer
06:31
as that's a belongs to.
06:32
Our director, that's also a belongs to.
06:35
Our cast members and our crew members,
06:37
which are both many to many relationships.
06:39
And if applicable, any watch lists
06:42
that this movie is a part of.
06:43
So if we hit delete there,
06:45
we get redirected right on back,
06:47
and we still see the movie.
06:48
We jump into our terminal,
06:50
invalid or expired CSRF token.
06:52
Forgot to add that into our form.
06:53
So let's jump back into our text editor here.
06:56
Let's go back into our index page.
06:58
We have our form right here.
07:00
Within that form, we need to apply our CSRF field.
07:04
Forgot about that.
07:05
Let's add that in now.
07:06
We just saw our browser refresh there in the background.
07:09
So now we should be good to go ahead
07:10
and try to hit delete once more.
07:12
And there we go.
07:13
Our movie is now gone.
07:14
It no longer exists.
07:16
It's not in this table at all.
07:18
And any relationships for it
07:19
have been automatically cascade deleted for us.
07:22
Let's do a couple more just to test it out.
07:23
So let's delete my cool movie two,
07:25
my cool movie one,
07:26
as well as some of the movies
07:27
that we created with our Cedar.
07:30
So I'm your boogeyman, that's gone.
07:32
Me and Miss Jones, that's gone.
07:33
Smooth, gone.
07:34
So awesome.
07:35
Everything seems to be deleting A-okay.
07:37
And we now have the full administrative crud flow
07:40
for our movies completed.
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!