1 00:00:01,240 --> 00:00:08,050 We have seen so far how Laravel implements restful routing and 2 00:00:08,050 --> 00:00:12,300 resourceful controllers to provide powerful access to our data. 3 00:00:12,300 --> 00:00:15,370 Even more impressive is how we can take this one step further 4 00:00:15,370 --> 00:00:18,510 by nesting our routes, as well as the related controllers, 5 00:00:18,510 --> 00:00:21,100 to cleanly grow our application. 6 00:00:21,100 --> 00:00:24,930 This is where we'll complete the first stage of our journey into Laravel. 7 00:00:24,930 --> 00:00:29,720 So next up, we're going to need to do more routes and more controllers. 8 00:00:29,720 --> 00:00:33,780 So another route for editing our tasks, or 9 00:00:33,780 --> 00:00:37,810 editing our items, destroying those items, creating new items. 10 00:00:37,810 --> 00:00:40,680 So all of the things that we've done before, with the actual To Do lists, 11 00:00:40,680 --> 00:00:44,840 we're gonna need to do the same thing with the To Do items. 12 00:00:44,840 --> 00:00:47,020 Now, the way we're gonna need to do this, and 13 00:00:47,020 --> 00:00:51,670 stay restful, is to use nested restful routes. 14 00:00:51,670 --> 00:00:54,180 Laravel makes this very easy for us. 15 00:00:54,180 --> 00:00:57,644 Let's do a quick review of the rest that we have so 16 00:00:57,644 --> 00:01:01,090 far by looking at our artisan routes command. 17 00:01:01,090 --> 00:01:03,220 So let's switch back over to our terminal. 18 00:01:03,220 --> 00:01:07,400 Head to our actual sites, Laravel project. 19 00:01:07,400 --> 00:01:12,320 And then we're gonna run PHP, artisan, and then routes. 20 00:01:14,120 --> 00:01:17,825 Let's make this output a little bit smaller here and 21 00:01:17,825 --> 00:01:20,800 then that way it should be a little easier to read. 22 00:01:20,800 --> 00:01:25,520 So here we have our normal rest that we have for our to-do lists. 23 00:01:25,520 --> 00:01:31,340 So you see we've got our create, we've got our store, show, edit, update and destroy. 24 00:01:31,340 --> 00:01:32,170 This is great. 25 00:01:32,170 --> 00:01:33,290 This is exactly what we need. 26 00:01:33,290 --> 00:01:34,780 We still have one for DB. 27 00:01:34,780 --> 00:01:38,480 Let's actually go ahead and get rid of that route just to clear up any confusion. 28 00:01:38,480 --> 00:01:42,250 So in our routes, here's DB, I'm gonna eliminate this. 29 00:01:42,250 --> 00:01:43,380 We don't need it. 30 00:01:43,380 --> 00:01:45,874 We actually don't need these two anymore. 31 00:01:45,874 --> 00:01:49,670 We'll leave the listener in there for now cuz we will need it later. 32 00:01:49,670 --> 00:01:53,530 Okay. So now let's add some new routes in 33 00:01:53,530 --> 00:01:57,100 by doing the same thing here for TodoList Controller, but 34 00:01:57,100 --> 00:02:01,630 we're gonna do todo item, so we're just gonna actually call this items. 35 00:02:03,595 --> 00:02:06,605 And then we'll do to do item controller. 36 00:02:09,095 --> 00:02:10,365 Which we don't have that yet but 37 00:02:10,365 --> 00:02:12,945 don't worry we're just looking at the routes for now. 38 00:02:12,945 --> 00:02:15,995 So we go back and we run the same command again, 39 00:02:15,995 --> 00:02:20,074 you'll see we have a whole new slew of commands that are instead of to dos and 40 00:02:20,074 --> 00:02:24,122 .destroy, it' items.destroy or items.index. 41 00:02:24,122 --> 00:02:25,535 Items.create. 42 00:02:25,535 --> 00:02:26,870 So these are great. 43 00:02:26,870 --> 00:02:28,910 We won't need all of these and 44 00:02:28,910 --> 00:02:33,700 we actually need them to be nested inside of our to-do lists. 45 00:02:33,700 --> 00:02:38,100 So the result that we're looking for here in the nested routes is we wanna be able 46 00:02:38,100 --> 00:02:46,170 to say todo/1/items/2, like the second item here. 47 00:02:46,170 --> 00:02:48,300 And then maybe edit. 48 00:02:48,300 --> 00:02:51,520 So that's one of the routes,and that's how we would keep it restful. 49 00:02:51,520 --> 00:02:55,640 So let's do that through Laravel routing, using nested routes. 50 00:02:55,640 --> 00:03:01,340 Switch back over to your routes, and instead of saying route, resource, 51 00:03:01,340 --> 00:03:06,524 items, let's go ahead and say route, resource, todo.items. 52 00:03:06,524 --> 00:03:11,354 So to-dos, same as before, to-dos.items. 53 00:03:11,354 --> 00:03:14,310 And then it's going to be item controller. 54 00:03:14,310 --> 00:03:16,710 So save that, and 55 00:03:16,710 --> 00:03:21,910 we'll go back and run our artisan routes again and see what it looks like now. 56 00:03:21,910 --> 00:03:22,860 So, let's take a look. 57 00:03:22,860 --> 00:03:26,230 At the one that we were using as an example which is our edit. 58 00:03:26,230 --> 00:03:30,560 So, here we're using a GET, it'll be /todos, the to do list ID, 59 00:03:30,560 --> 00:03:35,390 it'll be /items, and then the item ID and /edit. 60 00:03:35,390 --> 00:03:40,480 And now, our route instead of saying items edit, says todos.items.edit, but yet it's 61 00:03:40,480 --> 00:03:45,330 still going to the to do item controller which is exactly what we want it to do. 62 00:03:45,330 --> 00:03:49,070 Which is separating our item controller from our list controller. 63 00:03:49,070 --> 00:03:51,780 For sure we can eliminate some of these actions. 64 00:03:51,780 --> 00:03:55,910 We don't need to look at a list of all of the items 65 00:03:55,910 --> 00:04:01,160 separately using the to do item controller because that's actually being done on the. 66 00:04:01,160 --> 00:04:03,560 Todos, and then showing all of them. 67 00:04:03,560 --> 00:04:06,110 So it's gonna show the list, which will show the title, and 68 00:04:06,110 --> 00:04:11,920 then we're using our nested routes or related routes to show all of our items. 69 00:04:11,920 --> 00:04:13,840 So let's go ahead and try to eliminate that now. 70 00:04:13,840 --> 00:04:16,760 And we can do it from within the actual route file. 71 00:04:16,760 --> 00:04:20,550 So when we're doing resource and then passing through our arguments. 72 00:04:20,550 --> 00:04:24,880 We have our named route, or what we're calling our routes. 73 00:04:24,880 --> 00:04:26,840 And then we have our controller. 74 00:04:26,840 --> 00:04:29,320 We can pass through another argument. 75 00:04:29,320 --> 00:04:33,490 Which is going to be an array of options and it will be for us, accept. 76 00:04:33,490 --> 00:04:37,380 So here we're gonna say accept a particular route. 77 00:04:37,380 --> 00:04:38,170 Accept index. 78 00:04:38,170 --> 00:04:43,560 We might want more but this particular argument actually needs to see an array. 79 00:04:43,560 --> 00:04:45,580 Of actions that we don't need. 80 00:04:45,580 --> 00:04:50,290 So here, we're passing through accep and then array, but it only has one item for 81 00:04:50,290 --> 00:04:51,930 now, which is index. 82 00:04:51,930 --> 00:04:53,300 So we'll save that. 83 00:04:53,300 --> 00:04:59,440 We'll switch back over to our artisan here, and run the routes again. 84 00:04:59,440 --> 00:05:01,760 So, now we don't have an index action. 85 00:05:01,760 --> 00:05:06,230 Another one that we're not gonna need is to show the individual action itself. 86 00:05:06,230 --> 00:05:10,550 We definitely wanna destroy an item, we definitely wanna update an item. 87 00:05:10,550 --> 00:05:12,300 We certainly would like to edit an item. 88 00:05:12,300 --> 00:05:14,450 Do we wanna show an item? 89 00:05:14,450 --> 00:05:15,080 No. 90 00:05:15,080 --> 00:05:16,210 Do we wanna store? 91 00:05:16,210 --> 00:05:16,740 Yes. 92 00:05:16,740 --> 00:05:17,800 Do we wanna create? 93 00:05:17,800 --> 00:05:18,710 Absolutely. 94 00:05:18,710 --> 00:05:21,320 So the only one that's still there, that is extra or 95 00:05:21,320 --> 00:05:23,820 unneeded, would be our show action. 96 00:05:23,820 --> 00:05:26,160 So simply go back over to your routes page, and 97 00:05:26,160 --> 00:05:29,000 add a comma and then another option here, which would be show. 98 00:05:30,790 --> 00:05:32,540 Refresh our routes again. 99 00:05:32,540 --> 00:05:33,400 Make sure eliminated it. 100 00:05:34,620 --> 00:05:35,230 And it did. 101 00:05:35,230 --> 00:05:35,730 Great.