1 00:00:00,440 --> 00:00:01,590 Nice job. 2 00:00:01,590 --> 00:00:06,750 We created a route for our authors in books which successfully returned a list 3 00:00:06,750 --> 00:00:11,170 of authors from the author controller and books from the book controller. 4 00:00:12,220 --> 00:00:16,820 Now we're ready to create our resources for both our authors and books. 5 00:00:17,850 --> 00:00:23,226 Before we get started, let's take a look at the authors returned by our API. 6 00:00:23,226 --> 00:00:28,108 And you'll notice that in addition to the name, title, 7 00:00:28,108 --> 00:00:32,886 company, and email, we also have an id, created at and 8 00:00:32,886 --> 00:00:38,298 updated at, which we don't want to show to our API consumers. 9 00:00:38,298 --> 00:00:43,493 Hiding database specific data is a best practice when building an API, 10 00:00:43,493 --> 00:00:47,131 but you can also use resources to hide passwords or 11 00:00:47,131 --> 00:00:51,549 other sensitive data which aren't database specific, but 12 00:00:51,549 --> 00:00:55,731 is an obvious advantage when building a secure REST API. 13 00:00:55,731 --> 00:00:59,144 In the next video, we'll create resources for 14 00:00:59,144 --> 00:01:02,729 our authors and books, while using the author and 15 00:01:02,729 --> 00:01:07,950 book models as guides to see exactly what data we want our API to display. 16 00:01:09,260 --> 00:01:13,278 Let's dive right in and create our first resource like this. 17 00:01:24,961 --> 00:01:29,425 By default, the resource method we just created will run 18 00:01:29,425 --> 00:01:34,360 a two array function, which gets and returns all attributes. 19 00:01:34,360 --> 00:01:36,431 To illustrate how this works, 20 00:01:36,431 --> 00:01:41,220 let's import the AuthorResource in the AuthorController like this. 21 00:01:43,664 --> 00:01:48,658 Now we can wrap the AuthorResource as a collection around the Author 22 00:01:48,658 --> 00:01:49,976 model like this. 23 00:02:06,222 --> 00:02:10,966 Moment of truth, let's test the authors like we did before using 24 00:02:10,966 --> 00:02:15,548 the welcome view or by copying and pasting the URL, like this. 25 00:02:18,691 --> 00:02:20,980 Notice how nothing changed. 26 00:02:20,980 --> 00:02:24,868 This is because we're simply passing in all resources, 27 00:02:24,868 --> 00:02:29,577 which we can change by defining only the resources we want to expose. 28 00:02:29,577 --> 00:02:37,503 To see this in action, let's only expose our author names using an array like this. 29 00:02:45,708 --> 00:02:48,890 Now, all we see are just author names. 30 00:02:48,890 --> 00:02:50,180 How cool is that? 31 00:02:50,180 --> 00:02:54,668 Let's keep going and finish the author resource by exposing 32 00:02:54,668 --> 00:02:59,156 the names as well as the title, company and email like this. 33 00:03:03,984 --> 00:03:06,961 Let's test the authors in the browser again. 34 00:03:06,961 --> 00:03:11,759 And notice how the author resource hides the database specific data. 35 00:03:11,759 --> 00:03:16,682 This is because we're only passing in the name, title, 36 00:03:16,682 --> 00:03:20,900 company and email without the ID or timestamps. 37 00:03:20,900 --> 00:03:22,190 Nice job. 38 00:03:23,910 --> 00:03:28,820 Great, we're now exposing only the data we actually want to return to our 39 00:03:28,820 --> 00:03:33,770 API consumers, without any of the database specific data. 40 00:03:33,770 --> 00:03:35,640 Mission accomplished. 41 00:03:35,640 --> 00:03:40,520 Now that our author controller at index function is done, let's start working 42 00:03:40,520 --> 00:03:44,620 on the author controller at show function in the next section. 43 00:03:44,620 --> 00:03:45,140 See you there. 44 00:03:46,430 --> 00:03:51,050 Before we can start working on the author controller at show function, 45 00:03:51,050 --> 00:03:53,110 we need to create the endpoint. 46 00:03:53,110 --> 00:03:59,288 So let's head over to the routes file api.php and add another route like this. 47 00:04:02,454 --> 00:04:06,443 Next, let's open the AuthorController.php file. 48 00:04:06,443 --> 00:04:10,961 And notice that we are simply grabbing the author dollar author and 49 00:04:10,961 --> 00:04:16,543 returning that author using response dollar author with a status code of 200. 50 00:04:18,616 --> 00:04:21,379 Now that we have our new route defined, 51 00:04:21,379 --> 00:04:26,594 let's view a list of all routes currently available in our API like this. 52 00:04:29,910 --> 00:04:33,850 Nice, as you can see, we have our routes defined. 53 00:04:33,850 --> 00:04:36,350 So let's keep testing our routes. 54 00:04:36,350 --> 00:04:39,980 You can use Postman, which we will do later in the course. 55 00:04:39,980 --> 00:04:44,095 But for now, make sure your API is running and 56 00:04:44,095 --> 00:04:48,537 navigate to the author's route in the browser, 57 00:04:48,537 --> 00:04:52,668 then simply add a one after the URL like this. 58 00:04:52,668 --> 00:04:55,501 Feel free to try two and three. 59 00:04:55,501 --> 00:04:58,639 But that's all of the authors we have so far. 60 00:04:58,639 --> 00:05:05,221 Using four and above will return a status code of 404 Not Found, which is expected. 61 00:05:08,531 --> 00:05:10,060 Great job. 62 00:05:10,060 --> 00:05:14,620 We're now showing one specific author and everything looks great, 63 00:05:14,620 --> 00:05:19,170 except that our one author is once again showing everything, 64 00:05:19,170 --> 00:05:21,850 such as the ID and timestamp data. 65 00:05:22,900 --> 00:05:27,434 Instead of returning everything in the to array function, 66 00:05:27,434 --> 00:05:33,329 like we demonstrated earlier, we can define each attribute of our author and 67 00:05:33,329 --> 00:05:38,152 book models within an array of the AuthorResource.php file. 68 00:05:38,152 --> 00:05:43,268 First, we need to open the author controller file and return a new 69 00:05:43,268 --> 00:05:49,145 author resource by wrapping the author inside of an HTTP status like this. 70 00:05:55,384 --> 00:05:59,941 Nice, remember, if you're working with one resource, 71 00:05:59,941 --> 00:06:05,149 use new author resource but when working with an array of authors, 72 00:06:05,149 --> 00:06:10,264 you need to return a collection like we did in the index function, 73 00:06:10,264 --> 00:06:14,940 which passes in the author all to get all authors. 74 00:06:14,940 --> 00:06:20,380 Remember, it's best practice to remove database specific information, 75 00:06:20,380 --> 00:06:24,547 such as the ID for example so everything is looking great. 76 00:06:24,547 --> 00:06:26,936 Let's view our work in the browser. 77 00:06:29,865 --> 00:06:35,534 Look at that, no ID or timestamp data, everything is looking great. 78 00:06:35,534 --> 00:06:40,024 Now that we're only exposing the attribute you want to show for 79 00:06:40,024 --> 00:06:44,948 the @show function, we can start building the @store function. 80 00:06:44,948 --> 00:06:49,580 But first, let's create the route like we did with the app index and 81 00:06:49,580 --> 00:06:51,703 add show functions like this. 82 00:06:54,181 --> 00:06:59,847 Now that we have our new route defined, let's view a list of routes like this. 83 00:07:03,173 --> 00:07:06,860 Great, it's there, as expected. 84 00:07:06,860 --> 00:07:10,264 Now, let's navigate into the author controller and 85 00:07:10,264 --> 00:07:14,508 take a look at the Add store function in the fields we want to store. 86 00:07:18,361 --> 00:07:25,769 Great, we now know that we want to expose the name, title, company and email. 87 00:07:25,769 --> 00:07:32,092 Next, we want to wrap the newly created author with the author resource like this. 88 00:07:42,405 --> 00:07:43,547 Great job. 89 00:07:43,547 --> 00:07:44,978 In the next section, 90 00:07:44,978 --> 00:07:50,312 we'll finally start testing our API endpoints using Postman and REST client. 91 00:07:50,312 --> 00:07:51,090 See you there.