00:00
[MUSIC]
00:05
Traditionally on these movie listing sites, the actual movie show page is usually in
00:08
charge of showing the writer, director, as well as the cast members, crew members,
00:12
and things like that.
00:13
So today, let's go ahead and add in our cast and
00:15
crew members to this page as well.
00:16
We'll go ahead and put them underneath the abstract, but
00:18
first we actually need to get them queried.
00:20
So let's hide our browser away, and this will be inside of our movies controller.
00:23
So open that up, scroll down to our show method, and
00:26
here's where we want to add those queries in.
00:28
So we'll probably have our cast members listed out first, so
00:30
let's go ahead and query them first.
00:32
Again, just like our capabilities of loading in our director and
00:34
writer, we could do this directly with the query builder.
00:37
However, here we're just doing a find by or fail.
00:40
So instead, we can reach through the relationship of our movie record and
00:43
get to them that way.
00:45
So we can do const cast equals await movie.related.
00:50
We're looking for our cast members, so we'll reach for our cast members
00:52
relationship and get the query builder specifically for this movie's cast members.
00:57
We'll provide those cast members into our page, and
01:00
then let's go ahead and jump into our page, and let's get them listed.
01:03
So we'll scroll underneath where we have our abstract, add in an extra div,
01:07
class, flex, flex wrap, justified between, gap of four.
01:12
And then inside of this div, let's do another div, class, width,
01:17
full, lg, width, one half, and let's go ahead and duplicate that.
01:21
So for the first one, we'll put our cast members, and then for
01:23
the second one, we'll put our crew members.
01:25
So we'll go ahead and label that accordingly.
01:27
So we'll do an h3 class, font, bold, text, lg, cast.
01:33
We'll copy that h3, paste it in this div, and switch it to crew.
01:37
Okay, now inside of each of these, we'll want to have our definition list
01:41
actually listing out our cast members.
01:43
So for this, we'll do another div, class, flex, gap of maybe two.
01:47
And then we're gonna want to loop over that div.
01:49
So we'll do @each, sinist, in, cast, and then we can end our each there.
01:56
We'll do our definition title.
01:57
This will be the actual sinist's name, so sinist.fullname.
02:01
And then we'll have the data definition.
02:03
This will be the person that they played.
02:06
We'll leave that empty for right now, and we'll take a look at what we have.
02:09
So let's jump back into our browser.
02:10
Okay, cool, so we see our cast members right down here.
02:12
So, so far, so good.
02:13
Let's hide that back away.
02:15
And let's take a look at what the actual data is that we have on each one of these
02:18
sinist records.
02:19
So underneath our top div here,
02:20
let's just scroll down to the bottom of the page, and let's inspect our cast.
02:25
Let's open our browser back up and scroll down to the bottom of the page.
02:28
All right, now we're seeing the underlying model instances for
02:31
each one of the cast members that we queried.
02:32
We see first name, last name, headshot URL, all that looks right.
02:36
We continue scrolling down.
02:37
We'll get down to our extras.
02:39
That's what we're looking for right here.
02:41
And we can see these pivot columns as we've previously covered
02:44
within this extras object.
02:46
So we can reach for our pivot character name to get the character name that this
02:50
particular sinist played within this movie.
02:52
But let's also note while we're here that we need to account for
02:54
the pivot sort order whenever we're actually querying the cast,
02:57
because we'll want the cast to be listed according to the sort order.
03:01
So we'll take care of that as well.
03:02
Okay, get that back away.
03:04
So we know within our data definition here,
03:06
we want to display the character that the sinist played.
03:09
And we just saw that we can do that via sinist.$extras.
03:15
And then this property is prefixed with pivot because it came from our pivot table.
03:19
And since it came from our pivot table and
03:21
we don't have a model representation of it,
03:23
the name of the property is as it's defined inside of the database,
03:26
which is in snake case.
03:28
So we'll do character_name.
03:31
All right, let's also make this a little bit muted.
03:32
So let's do class=text_slate, maybe 600.
03:36
Okay, let's take a look at that real quick and see how that looks.
03:39
All righty, so we have the sinist full name, and
03:41
then we have the character name that that sinist played within this movie.
03:44
I think we got that backwards.
03:45
I think most sites list the character name and then the actor, or
03:49
in our case, the sinist who played them.
03:51
So let's switch that real quick.
03:52
So we'll just cut that out of our dt, paste it into our dd, and
03:56
then cut that part out of our dd and paste it into our dt.
04:00
Also gonna make this text small and maybe 400,
04:03
just to give it a little bit of an extra difference.
04:06
Okay, jump back into our browser, that looks better.
04:08
We'll also add in an items baseline, just so
04:11
that the bottoms of both of these texts align.
04:14
Okay, so next we need to account for our sorting.
04:16
So so that we can easily see this, I'm just gonna map over our cast members,
04:20
cc, and then we'll just serialize it real quick, just so that we get all of the extra
04:24
properties for our model instance out of the way.
04:27
And we can just see the underlying properties that we're looking at.
04:29
Okay, so within our meta, pivot sort order, zero there.
04:32
Then we go to two, which is definitely not right, and then to one, and
04:36
then lastly to three.
04:38
So we definitely wanna fix the sort order here within our cast.
04:41
So let's hide that back away, jump back into our movies controller.
04:45
And the way that we can do this is by off of our query builder,
04:48
doing an order by, and then specifying that we wanna order by the pivot sort order.
04:54
Just the same as we're referencing the column inside of our extras object.
04:58
So if we give that a save, I wasn't happy with my formatting, but okay.
05:01
Jump back into our browser, give it a refresh, and there we go.
05:04
So now we have Damon Stanton, Nathan, Julian, and
05:06
Davin in the order with which they should have been.
05:09
So if we scroll through this now, we have our pivot sort order at zero,
05:12
then we go down to one, then two, and lastly, three.
05:16
So everything is listing out correctly there.
05:18
While we're on our pivot columns though, let's stop for a second.
05:21
So if we jump back into our movie model, the reason that these pivot columns are
05:26
plopped automatically on our extras and within our query results is because we
05:31
have the pivot columns defined on this relationship.
05:34
Here we're on our cast movies relationship for our cast members.
05:37
We did not apply that to our crew members.
05:40
So we're gonna need a way for our crew members to provide that we want their,
05:43
I believe it was their title for their job title for the movie,
05:47
as well as their sort order as well.
05:49
So within our movies controller here, whenever we go to replicate all of this
05:53
for our crew, if we just give that a copy, do our crew, provide our crew into our
05:57
page, we also need to switch that relationship to crew members.
06:02
Let's jump back into our page.
06:03
We'll switch this from cast to our crew.
06:05
We'll scroll up, give our definition list here a copy and a paste, and
06:10
switch our loop from cast to crew.
06:12
And for right now, let's just get rid of our DT.
06:15
Okay, we'll give that a save, jump back into our browser, and
06:18
you're gonna see immediately that we get an error because the column pivot sort
06:23
order does not exist within the queried fields.
06:26
It exists on our table, but Lucid didn't include it inside of its query,
06:29
therefore it doesn't actually know about it.
06:32
So we're gonna need to include it inside of that query in order for
06:35
us to order by it.
06:35
So we can jump back into our movies controller.
06:37
On our query, we'll break that down into a new line.
06:40
There is an option called pivot columns, where we can define the exact same
06:45
specification that we have on our movie model for the pivot columns right here.
06:49
We could just do it on a per query basis because we may not need this information
06:53
every time that we're querying our cast and crew.
06:56
So you have the option to either always include it via the relationship definition
07:00
or include it per query via the query builder.
07:03
So our crew members have a title as well as the sort order
07:07
that we'll want to pluck off of the pivot table.
07:10
Now with the sort order included as a pivot column, it should get included
07:14
inside of our query and allow us to order by that pivot sort order.
07:18
So if we give this a save, let's jump back into our browser real quick and
07:21
make sure that that fixed everything.
07:23
So we'll give this a refresh and cool, we're back.
07:26
We have our crew members now.
07:27
We don't have their title in there quite yet.
07:29
But if we scroll down, we'll see immediately in the meta,
07:32
the pivot title is available there.
07:34
So we can pluck it and it's readily available.
07:36
But our pivot sort order starts at zero now.
07:38
If we scroll down, it goes to one next, scroll down some more, it goes to two.
07:42
And that's the last one.
07:43
So everything's sorting there A-OK.
07:45
Let's scroll back up and let's get their title added in.
07:48
So we jump back into our show page.
07:50
Let's go back a couple of steps to get our DT back in here.
07:52
We're gonna want their full name to be the primary title for the definition.
07:56
So we'll copy that and paste that into the DT.
07:59
For the DD, we'll wanna do Cineast.$extras.pivot_title.
08:06
Since we're plucking a pivot column and
08:08
the column's name itself is title on the pivot table.
08:11
Jump back into our browser and there we go.
08:13
So now we have Caesar was a gaffer, Kelly was a sound mixer, and
08:17
Jocelyn was a sound mixer as well.
08:19
So now before we move on from pivot columns, let's go ahead and
08:21
talk about how we can actually filter by them as well.
08:23
So let's hide away our inspect call.
08:26
We're done with that there.
08:27
And let's jump back into our movies controller.
08:29
And we had two sound mixers and one gaffer within our crew.
08:32
So let's go ahead and filter our crew down to just sound mixers.
08:36
For an example, we can chain on our query builder, a method called where pivot.
08:41
And you'll see within the autocomplete here,
08:43
we have pretty much every representation of the where clauses available specifically
08:47
for pivot columns.
08:48
So almost anything that you can do with a where statement,
08:50
you can also do with a where statement for your pivot columns instead.
08:54
And essentially what that's going to do is prefix any column name that we provide
08:57
as the key with that pivot_ prefix that we've been working with so
09:02
far with our pivot columns, just like right here.
09:05
So we'll automatically add that in for
09:06
us whenever it's applying this where pivot clause.
09:09
So if we wanted to filter the crew members title down to just sound mixers,
09:14
we would just provide title here and Lucid will take care of the rest.
09:18
And then for the value, we can filter this down to sound mixers.
09:21
Actually, that was singular.
09:22
Let's fix that real quick.
09:23
Okay, let's jump back into our browser, give that a refresh.
09:25
And now our crew went from three people down to just these two.
09:29
We're down at the bottom of the page here and our gaffer is now gone.
09:32
If we remove that where pivot, jump back into our browser,
09:36
give it a refresh once more, our gaffer is back.
09:39
So know that when you're working with your many-to-many relationships,
09:41
you can use those where pivot clauses to filter down specifically by your pivot
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!