Closed eokwukwe closed 4 years ago
Hi Okwukwe,
Thank you for your question.
From what I can understand from your code, you have deviated a bit from the code in the book and are testing for a collection of books instead of a single book.
The reason your tests are failing is that you're not hitting the show
method in your case, but the index
method instead.
So to get your tests to pass, you'll have to add the allowedIncludes('authors')
to your index
method in the BooksController
, like this:
public function index()
{
$books = QueryBuilder::for(Book::class)
->allowedSorts([
'title',
'publication_year',
'created_at',
'updated_at'
])
->allowedIncludes('authors')
->jsonPaginate();
return new BooksCollection($books);
}
It's great to see that you're already trying to do things your own way, but we do recommend that you follow the book and not deviate too much until you have finished and been through all the material. It will make it much easier for you to understand and follow along and then later make the necessary changes to your API.
Thank you @ThomasNoergaard for your reply.
Yes, the test passed by chaining the allowedIncludes('authors')
method. However, I have not, at this moment, deviated any bit from the code in the book.
On page 362, the query builder was added to the index method in the BooksController
during the test for sorting and pagination. And that what it.
On page 404, the implementation of the relationships
part of the JSON:API specification started with the BooksRelationshipsTest
On page 435, the implementation of the related link was introduced.
On page 438, the implementation of the included top-level member that makes it possible to include the related resources defined in the relationship linkage data member in the relationships object was introduced. It started with testing for a single book. The first test is on page 441. Then on page 443, the show
method in the BooksController
was updated by using the query builder package and adding the allowedIncludes('authors')
method to make the tests pass.
public function show($book)
{
$query = QueryBuilder::for(Book::where('id', $book))
->allowedIncludes('authors')
->firstOrFail();
return new BooksResource($query);
}
which lead to making modifications to the toArray
method in the BooksResource
class.
Then on page 455, the implementation of the included top-level member to a collection was introduced. In fact, this is the statement in the paragraph before the subheading Collections:
Our test is green and passing now and we have successfully implemented included top-level member for single resources. However, what about when we need to include related resource objects for many resources in a collection? Let’s tackle that next.
Remember, that at this point, the index
method of the BooksController
has not been updated to cater for the test and was not updated in the remaining part of the chapter. Yet from the book, the tests passed. But when the same functionality was tested for a single book, the show
method was updated to reflect the test.
So, the mistake is from the book and not from me deviating from the code in the book.
ANOTHER POTENTIAL ERROR If you take a look in the book on page 411, the relationship route is:
Route::get('books/{book}/relationships/authors', function(){
return true;
})->name('books.relationships.authors');
notice that book
was used as the params
but in the links
values of the toArray
method in the BooksResource
on page 412, in the self
and related
values, the same params
was referenced by id
'links' => [
'self' => route(
'books.relationships.authors',
['id' => $this->id]
),
'related' => route(
'books.authors',
['id' => $this->id]
),
],
if you run the test at this point you get this error:
Illuminate\Routing\Exceptions\UrlGenerationException: Missing required parameters for [Route: books.relationships.authors] [URI: api/v1/books/{book}/relationships/authors].
I had to update mine to:
'links' => [
'self' => route(
'books.relationships.authors',
['book' => $this->id]
),
'related' => route(
'books.authors',
['book' => $this->id]
),
],
to make the test pass.
MY SUGGESTION In your next book update for Laravel 7, create a branch for the codes for each section. In that way, your readers can easily compare their code to yours at any point in time when they have errors that are not indicated in the book and figure out easily what went wrong. Remember some of your readers may be using your book to learn Laravel for the first time.
You're right, it has been noted and will be corrected in the next update, sorry.
The route()
helper method has been changed in one of the later releases of Laravel 6, with a breaking change where you now have to name the exact route parameter, like you're pointing out. This is something we're already addressing in the next update of the book, but thank you for notifying us about this.
We did consider doing it that way, since, like you said, it's a bit easier to see what has changed in each commit. We opted for the Step folder instead, to ensure that readers that might not be that familiar to GIT and GitHub, could still see each incremental step.
The following tests in the
BooksRelationshipTest.php
are failing:it_includes_related_resource_objects_for_a_collection_when_an_include_query_param_is_given
with this error:
it_only_includes_a_related_resource_object_once_for_a_collection
with this error:This is the link to my repo. Help will be appreciated.