This episode is for members only

Sign up to access "Enums in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
05. Preselected forms with enums

Transcript

00:00
Okay, let's implement the ability to edit a previous order and let's look at how we can map this up to the value that already exists within the model so we can pre-select a value
00:09
in that form. So over in our order edit controller we're just going ahead and passing this data down to a view at the moment, let's go ahead and pull in the order that we're going to get in here that we want to edit, and make sure we pull the namespace in for that, and let's just go ahead and pass that order down to the view. So let's come over to edit and let's just go ahead and
00:32
create an outer div here, let's say order id and put order hash and then the id and then down here, in fact let's wrap that as well, we'll create out a form which is going to contain that value. So we want this to go through to orders and order to patch this, so let's go ahead and do that now, and the method here is going to be post, we'll include our cross-site request forgery quickly,
01:03
we'll include our method spoofing, so we want to patch this, and then down here we want a select with all of the order statuses but we want the status for this particular order to already be chosen. So we can pretty much grab this from create.blade.php, so let's go ahead and grab this, we'll grab the status in here as well and the submit button, and we'll just go ahead and paste
01:25
these in. So this is now going to become edit, and these statuses we need to actually pass down as well. So over in our order edit controller, let's go ahead and pass these statuses down, and we know that comes from order status and cases. Great, so let's come over to here and let's say orders and just check our database, so let's say id of two just to keep things simple,
01:48
and edit, and there we go, order number two, we get our drop down and we get our edit button, which does nothing at the moment. So we need this to be pre-selected for the value that we currently have in there, now at the moment that is placed but let's change it over to shipped, and of course at the moment it's not going to show the right value, so we need to be able to
02:08
extract out the actual value of this to compare it to the item that we have in the database, and we're going to refactor this a little bit later just to make things a little bit easier with an accessor on our model, but let's go ahead and fill this in before we do that. So we know in here if we just dump out somewhere up here, we have an order status which is the string value
02:33
that's stored in the database that's shipped, so we need to match that up with the value that we have in here, so what we can do is on this option here we can go ahead and set a selected attribute, the Laravel blade has a selected helper that allows us to do this, and inside of here is just going to be the condition, so we want to say will the order status, which remember is that string,
02:55
we want to check if that is equal to status, but then we need to grab the value here somehow, to do that it's very very simple, and we kind of got a clue about this earlier, let's just check that out before we go any further, so I'm going to cut this out and let's head over to the order edit controller, and let's do a die dump on order status and cases, so if we come over to this
03:18
and have a look, name for each of the enums is the actual name of the enum, so this, and value is the value that we assigned, remember this is a backed enum and it has a value for each of our cases, so knowing that what we can now do, we just get rid of the die dump here, we can go over to our edit here, bring this back, and we can just simply say value, so does the string inside of the model
03:47
equal the string that we've assigned to value, which we know is always going to be in there, and sure enough shipped is now selected by default, so now that we've got this working let's finally just head over to our order update controller, and let's just fill this in, so we're going to go ahead and receive an order into here, as well as a request with the new data, in our case
04:11
just a status, and we're going to go ahead and say order update, and once again request only status, and then we're going to return back, now we need to be really careful here because of course over in our order store controller we were validating, we need to do exactly the same thing over here, a little bit of duplication here, but of course it doesn't matter too much,
04:34
and of course we could always move this out to a shared form request, okay let's check this out, I'm going to go ahead and update the status here to cancelled, and yeah it looks like our form isn't quite set up properly, so let's head over to edit, and have a look here, yeah of course that is not right, we actually need to output the order id here, so let's just say order id, and we should
04:53
be good, so let's change that to cancelled, and we actually get an error here, the selected status is invalid, so let's head over to our order update controller, it's probably because that we haven't pulled the order status namespace in, so the validation rule is taking a class that doesn't exist, and of course then it's not going to have access to our order status values, so let's just
05:15
try that again, hit cancelled, and there we go, it's been updated, it's persisting in the database now, and of course it's now showing by default on our form, so there we go, grabbing the current value, making sure that appears properly inside of the select as the selected option, and of course going ahead and updating something, also validating with our order status.
9 episodes 31 mins

Overview

Enums make working with multiple potential column values a breeze (think order statuses). Let's use an Enum in Laravel and cover storing, validating and accessing Enums.

By the end of the course, you'll be able to implement Enums in Laravel with ease, and make sure your multiple choice column data is strict and easy to globally update across your entire application.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.