WackyStudio / build-an-api-with-laravel

Official Build an API with Laravel repository
122 stars 56 forks source link

Test failing for BooksCollection Resource for the included top-level members #35

Closed eokwukwe closed 4 years ago

eokwukwe commented 4 years ago

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:

 Unable to find JSON:
[{
    "data": [
        {
            "id": "1",
            "type": "books",
            "attributes": {
                "title": "Dr. Freddie Kovacek",
                "description": "Ipsam eius harum nam.",
                "publication_year": "1982",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/1/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/1/authors"
                    },
                    "data": [
                        {
                            "id": "1",
                            "type": "authors"
                        },
                        {
                            "id": "2",
                            "type": "authors"
                        },
                        {
                            "id": "3",
                            "type": "authors"
                        }
                    ]
                }
            }
        },
        {
            "id": "2",
            "type": "books",
            "attributes": {
                "title": "Miss Aubree Bernier Sr.",
                "description": "Sed nulla tempora amet quod.",
                "publication_year": "1987",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/2/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/2/authors"
                    }
                }
            }
        },
        {
            "id": "3",
            "type": "books",
            "attributes": {
                "title": "Herman Bartoletti",
                "description": "Id beatae accusantium quam praesentium repellat ipsa maiores ut.",
                "publication_year": "1996",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/3/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/3/authors"
                    }
                }
            }
        }
    ],
    "included": [
        {
            "id": "1",
            "type": "authors",
            "attributes": {
                "first_name": "Terrance",
                "last_name": "Streich",
                "other_name": "Kohler",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            }
        },
        {
            "id": "2",
            "type": "authors",
            "attributes": {
                "first_name": "Emmitt",
                "last_name": "Walsh",
                "other_name": "O'Connell",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            }
        },
        {
            "id": "3",
            "type": "authors",
            "attributes": {
                "first_name": "Jeramy",
                "last_name": "Luettgen",
                "other_name": "Pacocha",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            }
        }
    ]
}]
within response JSON:
[{
    "data": [
        {
            "id": "1",
            "type": "books",
            "attributes": {
                "title": "Dr. Freddie Kovacek",
                "description": "Ipsam eius harum nam.",
                "publication_year": "1982",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/1/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/1/authors"
                    }
                }
            }
        },
        {
            "id": "2",
            "type": "books",
            "attributes": {
                "title": "Miss Aubree Bernier Sr.",
                "description": "Sed nulla tempora amet quod.",
                "publication_year": "1987",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/2/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/2/authors"
                    }
                }
            }
        },
        {
            "id": "3",
            "type": "books",
            "attributes": {
                "title": "Herman Bartoletti",
                "description": "Id beatae accusantium quam praesentium repellat ipsa maiores ut.",
                "publication_year": "1996",
                "created_at": "2020-06-06T22:23:22.000000Z",
                "updated_at": "2020-06-06T22:23:22.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/3/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/3/authors"
                    }
                }
            }
        }
    ],
    "links": {
        "first": "http://annas-bookstore.test/api/v1/books?include=authors&page%5Bnumber%5D=1",
        "last": "http://annas-bookstore.test/api/v1/books?include=authors&page%5Bnumber%5D=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://annas-bookstore.test/api/v1/books",
        "per_page": 30,
        "to": 3,
        "total": 3
    }
}].
Failed asserting that an array has the subset Array &0 (
    'data' => Array &1 (
        0 => Array &2 (
            'id' => '1'
            'type' => 'books'
            'attributes' => Array &3 (
                'title' => 'Dr. Freddie Kovacek'
                'description' => 'Ipsam eius harum nam.'
                'publication_year' => '1982'
                'created_at' => '2020-06-06T22:23:22.000000Z'
                'updated_at' => '2020-06-06T22:23:22.000000Z'
            )
            'relationships' => Array &4 (
                'authors' => Array &5 (
                    'links' => Array &6 (
                        'self' => 'http://annas-bookstore.test/api/v1/books/1/relationships/authors'
                        'related' => 'http://annas-bookstore.test/api/v1/books/1/authors'
                    )
                    'data' => Array &7 (
                        0 => Array &8 (
                            'id' => '1'
                            'type' => 'authors'
                        )
                        1 => Array &9 (
                            'id' => '2'
                            'type' => 'authors'
                        )
                        2 => Array &10 (
                            'id' => '3'
                            'type' => 'authors'
                        )
                    )
                )
            )
        )
        1 => Array &11 (
            'id' => '2'
            'type' => 'books'
            'attributes' => Array &12 (
                'title' => 'Miss Aubree Bernier Sr.'
                'description' => 'Sed nulla tempora amet quod.'
                'publication_year' => '1987'
                'created_at' => '2020-06-06T22:23:22.000000Z'
                'updated_at' => '2020-06-06T22:23:22.000000Z'
            )
            'relationships' => Array &13 (
                'authors' => Array &14 (
                    'links' => Array &15 (
                        'self' => 'http://annas-bookstore.test/api/v1/books/2/relationships/authors'
                        'related' => 'http://annas-bookstore.test/api/v1/books/2/authors'
                    )
                )
            )
        )
        2 => Array &16 (
            'id' => '3'
            'type' => 'books'
            'attributes' => Array &17 (
                'title' => 'Herman Bartoletti'
                'description' => 'Id beatae accusantium quam praesentium repellat ipsa maiores ut.'
                'publication_year' => '1996'
                'created_at' => '2020-06-06T22:23:22.000000Z'
                'updated_at' => '2020-06-06T22:23:22.000000Z'
            )
            'relationships' => Array &18 (
                'authors' => Array &19 (
                    'links' => Array &20 (
                        'self' => 'http://annas-bookstore.test/api/v1/books/3/relationships/authors'
                        'related' => 'http://annas-bookstore.test/api/v1/books/3/authors'
                    )
                )
            )
        )
    )
    'included' => Array &21 (
        0 => Array &22 (
            'id' => '1'
            'type' => 'authors'
            'attributes' => Array &23 (
                'first_name' => 'Terrance'
                'last_name' => 'Streich'
                'other_name' => 'Kohler'
                'created_at' => '2020-06-06T22:23:22.000000Z'
                'updated_at' => '2020-06-06T22:23:22.000000Z'
            )
        )
        1 => Array &24 (
            'id' => '2'
            'type' => 'authors'
            'attributes' => Array &25 (
                'first_name' => 'Emmitt'
                'last_name' => 'Walsh'
                'other_name' => 'O'Connell'
                'created_at' => '2020-06-06T22:23:22.000000Z'
                'updated_at' => '2020-06-06T22:23:22.000000Z'
            )
        )
        2 => Array &26 (
            'id' => '3'
            'type' => 'authors'
            'attributes' => Array &27 (
                'first_name' => 'Jeramy'
                'last_name' => 'Luettgen'
                'other_name' => 'Pacocha'
                'created_at' => '2020-06-06T22:23:22.000000Z'
                'updated_at' => '2020-06-06T22:23:22.000000Z'
            )
        )
    )
).
--- Expected
+++ Actual
@@ @@
             'self' => 'http://annas-bookstore.test/api/v1/books/1/relationships/authors',
             'related' => 'http://annas-bookstore.test/api/v1/books/1/authors',
           ),
-          'data' =>
-          array (
-            0 =>
-            array (
-              'id' => '1',
-              'type' => 'authors',
-            ),
-            1 =>
-            array (
-              'id' => '2',
-              'type' => 'authors',
-            ),
-            2 =>
-            array (
-              'id' => '3',
-              'type' => 'authors',
-            ),
-          ),
         ),
       ),
     ),
@@ @@
     'per_page' => 30,
     'to' => 3,
     'total' => 3,
-  ),
-  'included' =>
-  array (
-    0 =>
-    array (
-      'id' => '1',
-      'type' => 'authors',
-      'attributes' =>
-      array (
-        'first_name' => 'Terrance',
-        'last_name' => 'Streich',
-        'other_name' => 'Kohler',
-        'created_at' => '2020-06-06T22:23:22.000000Z',
-        'updated_at' => '2020-06-06T22:23:22.000000Z',
-      ),
-    ),
-    1 =>
-    array (
-      'id' => '2',
-      'type' => 'authors',
-      'attributes' =>
-      array (
-        'first_name' => 'Emmitt',
-        'last_name' => 'Walsh',
-        'other_name' => 'O\'Connell',
-        'created_at' => '2020-06-06T22:23:22.000000Z',
-        'updated_at' => '2020-06-06T22:23:22.000000Z',
-      ),
-    ),
-    2 =>
-    array (
-      'id' => '3',
-      'type' => 'authors',
-      'attributes' =>
-      array (
-        'first_name' => 'Jeramy',
-        'last_name' => 'Luettgen',
-        'other_name' => 'Pacocha',
-        'created_at' => '2020-06-06T22:23:22.000000Z',
-        'updated_at' => '2020-06-06T22:23:22.000000Z',
-      ),
-    ),
   ),
 )

it_only_includes_a_related_resource_object_once_for_a_collection with this error:

 Unable to find JSON:
[{
    "data": [
        {
            "id": "1",
            "type": "books",
            "attributes": {
                "title": "Prof. Baylee Morissette",
                "description": "Assumenda qui deleniti perferendis nihil architecto.",
                "publication_year": "2004",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/1/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/1/authors"
                    },
                    "data": [
                        {
                            "id": 1,
                            "type": "authors"
                        },
                        {
                            "id": 2,
                            "type": "authors"
                        },
                        {
                            "id": 3,
                            "type": "authors"
                        }
                    ]
                }
            }
        },
        {
            "id": "2",
            "type": "books",
            "attributes": {
                "title": "Idell Kilback V",
                "description": "Sed soluta eum corrupti blanditiis non ipsa rerum praesentium.",
                "publication_year": "1984",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/2/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/2/authors"
                    },
                    "data": [
                        {
                            "id": 1,
                            "type": "authors"
                        },
                        {
                            "id": 2,
                            "type": "authors"
                        },
                        {
                            "id": 3,
                            "type": "authors"
                        }
                    ]
                }
            }
        },
        {
            "id": "3",
            "type": "books",
            "attributes": {
                "title": "Delmer Padberg",
                "description": "Possimus aut dicta laudantium sint voluptates.",
                "publication_year": "2004",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/3/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/3/authors"
                    },
                    "data": [
                        {
                            "id": 1,
                            "type": "authors"
                        },
                        {
                            "id": 2,
                            "type": "authors"
                        },
                        {
                            "id": 3,
                            "type": "authors"
                        }
                    ]
                }
            }
        }
    ],
    "included": [
        {
            "id": "1",
            "type": "authors",
            "attributes": {
                "first_name": "Jefferey",
                "last_name": "Bradtke",
                "other_name": "Nikolaus",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            }
        },
        {
            "id": "2",
            "type": "authors",
            "attributes": {
                "first_name": "Randy",
                "last_name": "Bahringer",
                "other_name": "Trantow",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            }
        },
        {
            "id": "3",
            "type": "authors",
            "attributes": {
                "first_name": "Shannon",
                "last_name": "Quitzon",
                "other_name": "Von",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            }
        }
    ]
}]
within response JSON:
[{
    "data": [
        {
            "id": "1",
            "type": "books",
            "attributes": {
                "title": "Prof. Baylee Morissette",
                "description": "Assumenda qui deleniti perferendis nihil architecto.",
                "publication_year": "2004",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/1/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/1/authors"
                    }
                }
            }
        },
        {
            "id": "2",
            "type": "books",
            "attributes": {
                "title": "Idell Kilback V",
                "description": "Sed soluta eum corrupti blanditiis non ipsa rerum praesentium.",
                "publication_year": "1984",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/2/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/2/authors"
                    }
                }
            }
        },
        {
            "id": "3",
            "type": "books",
            "attributes": {
                "title": "Delmer Padberg",
                "description": "Possimus aut dicta laudantium sint voluptates.",
                "publication_year": "2004",
                "created_at": "2020-06-06T22:26:05.000000Z",
                "updated_at": "2020-06-06T22:26:05.000000Z"
            },
            "relationships": {
                "authors": {
                    "links": {
                        "self": "http://annas-bookstore.test/api/v1/books/3/relationships/authors",
                        "related": "http://annas-bookstore.test/api/v1/books/3/authors"
                    }
                }
            }
        }
    ],
    "links": {
        "first": "http://annas-bookstore.test/api/v1/books?include=authors&page%5Bnumber%5D=1",
        "last": "http://annas-bookstore.test/api/v1/books?include=authors&page%5Bnumber%5D=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://annas-bookstore.test/api/v1/books",
        "per_page": 30,
        "to": 3,
        "total": 3
    }
}].
Failed asserting that an array has the subset Array &0 (
    'data' => Array &1 (
        0 => Array &2 (
            'id' => '1'
            'type' => 'books'
            'attributes' => Array &3 (
                'title' => 'Prof. Baylee Morissette'
                'description' => 'Assumenda qui deleniti perferendis nihil architecto.'
                'publication_year' => '2004'
                'created_at' => '2020-06-06T22:26:05.000000Z'
                'updated_at' => '2020-06-06T22:26:05.000000Z'
            )
            'relationships' => Array &4 (
                'authors' => Array &5 (
                    'links' => Array &6 (
                        'self' => 'http://annas-bookstore.test/api/v1/books/1/relationships/authors'
                        'related' => 'http://annas-bookstore.test/api/v1/books/1/authors'
                    )
                    'data' => Array &7 (
                        0 => Array &8 (
                            'id' => 1
                            'type' => 'authors'
                        )
                        1 => Array &9 (
                            'id' => 2
                            'type' => 'authors'
                        )
                        2 => Array &10 (
                            'id' => 3
                            'type' => 'authors'
                        )
                    )
                )
            )
        )
        1 => Array &11 (
            'id' => '2'
            'type' => 'books'
            'attributes' => Array &12 (
                'title' => 'Idell Kilback V'
                'description' => 'Sed soluta eum corrupti blanditiis non ipsa rerum praesentium.'
                'publication_year' => '1984'
                'created_at' => '2020-06-06T22:26:05.000000Z'
                'updated_at' => '2020-06-06T22:26:05.000000Z'
            )
            'relationships' => Array &13 (
                'authors' => Array &14 (
                    'links' => Array &15 (
                        'self' => 'http://annas-bookstore.test/api/v1/books/2/relationships/authors'
                        'related' => 'http://annas-bookstore.test/api/v1/books/2/authors'
                    )
                    'data' => Array &16 (
                        0 => Array &17 (
                            'id' => 1
                            'type' => 'authors'
                        )
                        1 => Array &18 (
                            'id' => 2
                            'type' => 'authors'
                        )
                        2 => Array &19 (
                            'id' => 3
                            'type' => 'authors'
                        )
                    )
                )
            )
        )
        2 => Array &20 (
            'id' => '3'
            'type' => 'books'
            'attributes' => Array &21 (
                'title' => 'Delmer Padberg'
                'description' => 'Possimus aut dicta laudantium sint voluptates.'
                'publication_year' => '2004'
                'created_at' => '2020-06-06T22:26:05.000000Z'
                'updated_at' => '2020-06-06T22:26:05.000000Z'
            )
            'relationships' => Array &22 (
                'authors' => Array &23 (
                    'links' => Array &24 (
                        'self' => 'http://annas-bookstore.test/api/v1/books/3/relationships/authors'
                        'related' => 'http://annas-bookstore.test/api/v1/books/3/authors'
                    )
                    'data' => Array &25 (
                        0 => Array &26 (
                            'id' => 1
                            'type' => 'authors'
                        )
                        1 => Array &27 (
                            'id' => 2
                            'type' => 'authors'
                        )
                        2 => Array &28 (
                            'id' => 3
                            'type' => 'authors'
                        )
                    )
                )
            )
        )
    )
    'included' => Array &29 (
        0 => Array &30 (
            'id' => '1'
            'type' => 'authors'
            'attributes' => Array &31 (
                'first_name' => 'Jefferey'
                'last_name' => 'Bradtke'
                'other_name' => 'Nikolaus'
                'created_at' => '2020-06-06T22:26:05.000000Z'
                'updated_at' => '2020-06-06T22:26:05.000000Z'
            )
        )
        1 => Array &32 (
            'id' => '2'
            'type' => 'authors'
            'attributes' => Array &33 (
                'first_name' => 'Randy'
                'last_name' => 'Bahringer'
                'other_name' => 'Trantow'
                'created_at' => '2020-06-06T22:26:05.000000Z'
                'updated_at' => '2020-06-06T22:26:05.000000Z'
            )
        )
        2 => Array &34 (
            'id' => '3'
            'type' => 'authors'
            'attributes' => Array &35 (
                'first_name' => 'Shannon'
                'last_name' => 'Quitzon'
                'other_name' => 'Von'
                'created_at' => '2020-06-06T22:26:05.000000Z'
                'updated_at' => '2020-06-06T22:26:05.000000Z'
            )
        )
    )
).
--- Expected
+++ Actual
@@ @@
             'self' => 'http://annas-bookstore.test/api/v1/books/1/relationships/authors',
             'related' => 'http://annas-bookstore.test/api/v1/books/1/authors',
           ),
-          'data' =>
-          array (
-            0 =>
-            array (
-              'id' => 1,
-              'type' => 'authors',
-            ),
-            1 =>
-            array (
-              'id' => 2,
-              'type' => 'authors',
-            ),
-            2 =>
-            array (
-              'id' => 3,
-              'type' => 'authors',
-            ),
-          ),
         ),
       ),
     ),
@@ @@
             'self' => 'http://annas-bookstore.test/api/v1/books/2/relationships/authors',
             'related' => 'http://annas-bookstore.test/api/v1/books/2/authors',
           ),
-          'data' =>
-          array (
-            0 =>
-            array (
-              'id' => 1,
-              'type' => 'authors',
-            ),
-            1 =>
-            array (
-              'id' => 2,
-              'type' => 'authors',
-            ),
-            2 =>
-            array (
-              'id' => 3,
-              'type' => 'authors',
-            ),
-          ),
         ),
       ),
     ),
@@ @@
             'self' => 'http://annas-bookstore.test/api/v1/books/3/relationships/authors',
             'related' => 'http://annas-bookstore.test/api/v1/books/3/authors',
           ),
-          'data' =>
-          array (
-            0 =>
-            array (
-              'id' => 1,
-              'type' => 'authors',
-            ),
-            1 =>
-            array (
-              'id' => 2,
-              'type' => 'authors',
-            ),
-            2 =>
-            array (
-              'id' => 3,
-              'type' => 'authors',
-            ),
-          ),
         ),
       ),
     ),
@@ @@
     'per_page' => 30,
     'to' => 3,
     'total' => 3,
-  ),
-  'included' =>
-  array (
-    0 =>
-    array (
-      'id' => '1',
-      'type' => 'authors',
-      'attributes' =>
-      array (
-        'first_name' => 'Jefferey',
-        'last_name' => 'Bradtke',
-        'other_name' => 'Nikolaus',
-        'created_at' => '2020-06-06T22:26:05.000000Z',
-        'updated_at' => '2020-06-06T22:26:05.000000Z',
-      ),
-    ),
-    1 =>
-    array (
-      'id' => '2',
-      'type' => 'authors',
-      'attributes' =>
-      array (
-        'first_name' => 'Randy',
-        'last_name' => 'Bahringer',
-        'other_name' => 'Trantow',
-        'created_at' => '2020-06-06T22:26:05.000000Z',
-        'updated_at' => '2020-06-06T22:26:05.000000Z',
-      ),
-    ),
-    2 =>
-    array (
-      'id' => '3',
-      'type' => 'authors',
-      'attributes' =>
-      array (
-        'first_name' => 'Shannon',
-        'last_name' => 'Quitzon',
-        'other_name' => 'Von',
-        'created_at' => '2020-06-06T22:26:05.000000Z',
-        'updated_at' => '2020-06-06T22:26:05.000000Z',
-      ),
-    ),
   ),
 )

This is the link to my repo. Help will be appreciated.

ThomasNoergaard commented 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.

eokwukwe commented 4 years ago

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.

ThomasNoergaard commented 4 years ago

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.