Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this section, we’ll learn how we access and display specific data using Resources for our Authors and Books resources.
Eloquent
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Michael Cook
2,125 PointsWhy does the video never actually explain how to filter out database-specific information? Did I miss something?
Posted by Michael CookMichael Cook
2,125 Points0 Answers
-
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Welcome back.
0:05
We've created our resource controllers,
validated requests and
0:06
finally, we returned responses for both
the author controller and book controller.
0:10
In this section, we'll learn how we can
access specific data using resources.
0:16
Then we'll build a few basic routes so we
can test our authors and books resources.
0:22
Finally, we'll test our API so
0:28
we can verify the resources are displaying
the correct data as expected.
0:31
Why exactly do we want to use resources?
0:36
To answer this question,
we'll examine each piece of data and
0:40
decide which specific pieces will
be displayed to the API consumer.
0:43
For example,
it's best practice to hide author IDs,
0:49
timestamps, and passwords because
we don't want to share sensitive or
0:53
irrelevant data to the users of our API.
0:58
It's also worth noting that exposing
author IDs can give your API consumers
1:02
an idea of the size of your database,
which you may not want to make public.
1:07
Similarly, exposing timestamps
to end users of our API
1:12
is also not a common practice
simply because it's not relevant
1:17
to the end users who are accessing
authors and their books.
1:21
Last but not least,
you don't want to display passwords and
1:27
other sensitive data so
we'll explore how to use resources,
1:30
to expose only the data we want
to display to our API consumers.
1:35
Before we can create resources
to show specific data,
1:40
we will need to build some basic
routing so we can test and
1:44
verify exactly what data is
being exposed by the resource.
1:47
First, navigate to the routes directory
and open the api.php file inside.
1:53
Since we're building a REST API, we'll use
the api.php file instead of the web.php
2:00
file, which is used for building web apps
like we did in the Laravel basics course.
2:06
Next, delete the existing route and
let's create both the authors and
2:13
books routes with a get request like this.
2:18
Next, let's navigate to
the app/http/controllersdirectory and
2:50
open the AuthorController.php file.
2:56
The route we just created,
will access the @index function inside of
3:01
the AuthorController.php file,
and will return all authors
3:06
along with a status code of 200,
which means the request was successful.
3:11
We can verify this behavior using
a super handy artisan command like this.
3:16
This command shows all of
the routes used by our API.
3:25
Super handy!
3:30
Next, let's run the php artisan serve
command and be sure to use two terminals,
3:32
so we can continue working on our
API while we test our routes.
3:38
To open a new terminal,
3:43
click on the plus sign next to
the current terminal like this.
3:44
Finally, let's test our routes and
4:02
see if we get the data that we
expect from the index function.
4:04
All authors and a status code of 200 for
success like this.
4:09
Open the welcome view and
4:14
click on the authors which will
test the following routes.
4:16
And will show us all authors.
4:23
Next, try the books route like this.
4:27
And you will see a list of books.
4:32
If you saw both lists of authors and
books, way to go.
4:35
Otherwise, check your route and author
controller for typos or missing steps.
4:40
Great, we are now exposing only the data
we actually want to return to our
4:47
API consumers.
4:51
Without any of the database specific data,
mission accomplished.
4:53
Now that our author controller at index
function is done, let's start working
4:59
on the author controller at show
function in the next section.
5:04
See you there.
5:08
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up