1 00:00:00,154 --> 00:00:02,500 We're making great progress. 2 00:00:02,500 --> 00:00:04,760 We created the AuthorResource and 3 00:00:04,760 --> 00:00:09,560 used it to remove database-specific data such as ID and timestamps. 4 00:00:10,700 --> 00:00:14,430 Then we tested our routes using both Postman and 5 00:00:14,430 --> 00:00:19,340 REST client in order to verify that the data we are exposing is correct. 6 00:00:20,550 --> 00:00:24,020 In this section, we're going to repeat the steps we took 7 00:00:24,020 --> 00:00:28,720 when building the AuthorResource by using the book model as a guide. 8 00:00:28,720 --> 00:00:29,391 See you there. 9 00:00:31,303 --> 00:00:34,336 Before we get started building our BookResource, 10 00:00:34,336 --> 00:00:37,250 let's create our routes first. 11 00:00:37,250 --> 00:00:40,581 Navigate to the api.php file and copy and 12 00:00:40,581 --> 00:00:46,211 paste all except the index route in the author's routes, like this. 13 00:00:47,930 --> 00:00:49,984 Holding down the Option key and 14 00:00:49,984 --> 00:00:55,050 clicking allows you to edit multiple fields at once, which is super handy. 15 00:00:56,550 --> 00:01:01,004 Holding down the Shift and right arrow will select them all at the same time, 16 00:01:01,004 --> 00:01:02,408 allowing us to do this. 17 00:01:29,657 --> 00:01:32,131 Now that we have all of our routes defined, 18 00:01:32,131 --> 00:01:36,133 let's verify our routes with the route:list command, like this. 19 00:01:41,314 --> 00:01:45,060 Great, all of our BookController routes are defined. 20 00:01:45,060 --> 00:01:49,409 But we still need to refactor our BookController, store, and 21 00:01:49,409 --> 00:01:54,835 update functions, like we did with the AuthorController.php file before. 22 00:02:50,441 --> 00:02:55,402 Great, now we're ready to start building our BookResource using this artisan 23 00:02:55,402 --> 00:02:56,077 command. 24 00:03:01,559 --> 00:03:04,060 Ctrl+K will clear the terminal. 25 00:03:10,917 --> 00:03:15,860 Php artisan make:resource BookResource. 26 00:03:19,253 --> 00:03:23,995 Next, let's open the BookResource file located in 27 00:03:23,995 --> 00:03:28,090 the App/Http/Resources directory. 28 00:03:28,090 --> 00:03:29,525 And let's see what's happening there. 29 00:03:31,967 --> 00:03:36,900 If we run this code, you'll see that it returns all books. 30 00:03:36,900 --> 00:03:40,412 We can test this behavior using REST client, like this. 31 00:03:44,649 --> 00:03:46,240 Great job. 32 00:03:46,240 --> 00:03:52,309 Now we're ready to import the BookResource in the BookController.php file, like this. 33 00:04:04,460 --> 00:04:10,180 Did you notice the database-specific data such as IDs and timestamps? 34 00:04:10,180 --> 00:04:14,630 Great, now we'll delete the return statement in the BookResource, 35 00:04:14,630 --> 00:04:17,010 just like we did with the AuthorResource. 36 00:04:17,010 --> 00:04:24,123 And we'll 37 00:04:24,123 --> 00:04:32,026 use the book 38 00:04:32,026 --> 00:04:37,558 model as 39 00:04:37,558 --> 00:04:43,880 a guide to 40 00:04:43,880 --> 00:04:50,993 create our 41 00:04:50,993 --> 00:05:00,993 BookResource, 42 00:05:02,058 --> 00:05:09,968 like this. 43 00:05:26,767 --> 00:05:31,448 Next, we need to wrap our book model around the BookResource collection in 44 00:05:31,448 --> 00:05:33,617 the BookController, like this. 45 00:05:37,450 --> 00:05:42,562 Great, we're now ready to test our work with REST client using 46 00:05:42,562 --> 00:05:47,490 the rest_test.http file we created earlier, like this. 47 00:05:49,068 --> 00:05:53,940 Fantastic, our BookResource is removing the database-specific data 48 00:05:53,940 --> 00:05:59,310 because we are only exposing the fields we want to share with our API consumers, 49 00:05:59,310 --> 00:06:03,800 mainly authors and books with our IDs or timestamps. 50 00:06:05,360 --> 00:06:11,240 Our show function works great, and we tested this behavior using REST client. 51 00:06:11,240 --> 00:06:14,840 However, our single books are still returning the ID and 52 00:06:14,840 --> 00:06:20,710 timestamps again, and we don't want to expose this data to our API consumers. 53 00:06:20,710 --> 00:06:23,490 Fortunately, we can use the AuthorResource to, 54 00:06:23,490 --> 00:06:27,960 again, expose only the relevant data, like this. 55 00:06:27,960 --> 00:06:31,114 Let's take a look at the BookResource and why this works. 56 00:06:35,284 --> 00:06:37,213 We're basically passing in, 57 00:06:40,852 --> 00:06:45,789 $this->author, and this is why we are still getting the IDs and 58 00:06:45,789 --> 00:06:50,390 timestamps, because we're not using the AuthorResource. 59 00:06:52,450 --> 00:06:58,451 Using this block of code will use a new AuthorResource and 60 00:06:58,451 --> 00:07:01,707 passes in $this->author. 61 00:07:01,707 --> 00:07:04,689 Nice, to illustrate the power of resources, 62 00:07:04,689 --> 00:07:08,060 let's add a field to our BookResource, like this. 63 00:07:09,950 --> 00:07:13,377 Let's test our behavior using REST client once again. 64 00:07:21,754 --> 00:07:24,159 No IDs or timestamp. 65 00:07:26,596 --> 00:07:29,710 And notice how every book has the secret. 66 00:07:31,460 --> 00:07:35,240 Notice how this gets populated to every single book. 67 00:07:35,240 --> 00:07:41,410 This is where you can define what your API exposes using existing resources. 68 00:07:41,410 --> 00:07:42,640 Pretty powerful stuff. 69 00:07:45,290 --> 00:07:49,490 Congratulations, we're done building the core of our REST API. 70 00:07:50,520 --> 00:07:53,650 We tested our routes, and all of our data looks great. 71 00:07:54,680 --> 00:07:59,830 We even used our existing AuthorResource to remove the database-specific data 72 00:07:59,830 --> 00:08:01,370 when displaying books. 73 00:08:01,370 --> 00:08:02,870 Way to go. 74 00:08:02,870 --> 00:08:08,300 In the next section, we're going to clean up both our authors and books routes. 75 00:08:08,300 --> 00:08:13,610 Then we'll discuss validation messages and how we can make them even better. 76 00:08:13,610 --> 00:08:14,110 See you there.