Comments (3)
Hi Saneesh,
The reason you’re getting the exception is due to changes in the Laravel framework.
We wrote the book when the current version of Laravel was 5.8 and in-between one of the later releases of Laravel 6 and Laravel 7, the way Laravel handles route parameters was changed.
Now you have to give the exact name of the route parameter and are not able to use the id
key anymore, when using the route()
helper function.
To fix this, you should make the following change in the BookResource class:
class BookResource extends JsonResource {
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request) {
return [
'id' => (string) $this->id,
'type' => 'books',
'attributes' => [
'title' => $this->title,
'description' => $this->description,
'publication_year' => $this->publication_year,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
],
'relationships' => [
'authors' => [
'links' => [
'self' => route('books.relationships.authors', [
'book' => $this->id,
]),
'related' => route('books.authors', [
'book' => $this->id,
]),
],
'data' => $this->authors->map(function ($author) {
return [
'id' => $author->id,
'type' => 'authors',
];
}),
],
],
];
}
}
Notice how the id
key in the route()
helper function has been changed to book
.
The same has to be done in the test:
/**
* @test
*/
public function it_returns_a_relationship_to_authors_adhering_to_json_api_specification() {
$this->withoutExceptionHandling();
$book = factory(Book::class)->create();
$authors = factory(Author::class, 3)->create();
$book->authors()->sync($authors->only('id'));
$user = factory(User::class)->create();
Passport::actingAs($user);
$this->getJson('/api/v1/books/1', [
'accept' => 'application/vnd.api+json',
'content-type' => 'application/vnd.api+json',
])
->assertStatus(200)
->assertJson([
'data' => [
'id' => '1',
'type' => 'books',
'relationships' => [
'authors' => [
'links' => [
'self' => route('books.relationships.authors', [
'book' => $book->id,
]),
'related' => route('books.authors', [
'book' => $book->id,
]),
],
'data' => [
[
'id' => $authors->get(0)->id,
'type' => 'authors',
],
[
'id' => $authors->get(1)->id,
'type' => 'authors',
],
],
],
],
],
]);
}
We are in the midst of updating the book to Laravel 7 and will be releasing the updated version as soon as possible.
from build-an-api-with-laravel.
@ThomasNoergaard
First of all you guys are superb! :-)
Your solution is working! and my quick fix was for all route():
'self' => route('books.relationships.authors', [$book->id]),
Typo in the book:
You might be already noticed, many test case function names contain an_book
it should be a_book
.
from build-an-api-with-laravel.
Thank you very much, we're glad we could help.
Thank you, we will note that and correct it in the updated version :-)
from build-an-api-with-laravel.
Related Issues (8)
- Question regarding Unique Email Users HOT 4
- Unsupported grant type
- Test failing for BooksCollection Resource for the included top-level members HOT 3
- Unique constraint is not working from jsonapi config file HOT 9
- Call to a member function first on null HOT 3
- Polymorphism HOT 6
- Configuration files are not serializable HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from build-an-api-with-laravel.