GithubHelp home page GithubHelp logo

Illuminate\Routing\Exceptions\UrlGenerationException Missing required parameters for [Route: books.relationships.authors] [URI: api/v1/books/{book}/relationships/authors]. about build-an-api-with-laravel HOT 3 CLOSED

wackystudio avatar wackystudio commented on July 17, 2024
Illuminate\Routing\Exceptions\UrlGenerationException Missing required parameters for [Route: books.relationships.authors] [URI: api/v1/books/{book}/relationships/authors].

from build-an-api-with-laravel.

Comments (3)

ThomasNoergaard avatar ThomasNoergaard commented on July 17, 2024

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 idkey 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 idkey 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.

Saneesh avatar Saneesh commented on July 17, 2024

@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.

ThomasNoergaard avatar ThomasNoergaard commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.