title: Laravel 7 - Scoping problem in Nested Resource Route
body: <h2>Routes:</h2>
  <p>I have a nested resource route definition like this:</p>
  <pre><code>Route::resource('posts.comments', 'CommentController');
  </code></pre>
  <p>That produces the following routes:</p>
  <pre><code>+--------+-----------+--------------------------------------+------------------------+------------------------------------------------+------------+
  | Domain | Method    | URI                                  | Name                   | Action                                         | Middleware |
  +--------+-----------+--------------------------------------+------------------------+------------------------------------------------+------------+
  |        | GET|HEAD  | posts/{post}/comments                | posts.comments.index   | App\Http\Controllers\CommentController@index   | web        |
  |        | POST      | posts/{post}/comments                | posts.comments.store   | App\Http\Controllers\CommentController@store   | web        |
  |        | GET|HEAD  | posts/{post}/comments/create         | posts.comments.create  | App\Http\Controllers\CommentController@create  | web        |
  |        | GET|HEAD  | posts/{post}/comments/{comment}      | posts.comments.show    | App\Http\Controllers\CommentController@show    | web        |
  |        | PUT|PATCH | posts/{post}/comments/{comment}      | posts.comments.update  | App\Http\Controllers\CommentController@update  | web        |
  |        | DELETE    | posts/{post}/comments/{comment}      | posts.comments.destroy | App\Http\Controllers\CommentController@destroy | web        |
  |        | GET|HEAD  | posts/{post}/comments/{comment}/edit | posts.comments.edit    | App\Http\Controllers\CommentController@edit    | web        |
  +--------+-----------+--------------------------------------+------------------------+------------------------------------------------+------------+
  </code></pre>
  <hr />
  <h2>Relationships (In Models):</h2>
  <p><code>Post</code> model:</p>
  <pre><code>public function comments()
  {
      return $this-&gt;hasMany(Comment::class);
  }
  </code></pre>
  <p><code>Comment</code> model:</p>
  <pre><code>public function post()
  {
      return $this-&gt;belongsTo(Post::class);
  }
  </code></pre>
  <hr />
  <h2>Dummy Data (In Tables):</h2>
  <p><code>posts</code> table:</p>
  <pre><code>+----+--------+-----------------------------+---------------------+---------------------+
  | id | title  | body                        | created_at          | updated_at          |
  +----+--------+-----------------------------+---------------------+---------------------+
  | 1  | Post 1 | This is the body of Post 1. | 2020-07-29 11:20:53 | 2020-07-29 11:20:53 |
  | 2  | Post 2 | This is the body of Post 2. | 2020-07-29 11:21:13 | 2020-07-29 11:21:13 |
  +----+--------+-----------------------------+---------------------+---------------------+
  </code></pre>
  <p><code>comments</code> table:</p>
  <pre><code>+----+---------+-----------------------------+---------------------+---------------------+
  | id | post_id | body                        | created_at          | updated_at          |
  +----+---------+-----------------------------+---------------------+---------------------+
  | 1  | 1       | The comment for the Post 1. | 2020-07-29 11:22:27 | 2020-07-29 11:22:27 |
  | 2  | 2       | The comment for the Post 2. | 2020-07-29 11:22:32 | 2020-07-29 11:22:32 |
  +----+---------+-----------------------------+---------------------+---------------------+
  </code></pre>
  <hr />
  <p>In the <a href="https://laravel.com/docs/7.x/routing#implicit-binding" rel="noreferrer">docs</a>:</p>
  <blockquote>
  <p>When using a custom keyed implicit binding as a nested route
  parameter, Laravel will automatically scope the query to retrieve the
  nested model by its parent using conventions to guess the relationship
  name on the parent.</p>
  </blockquote>
  <p>So, <code>{comment}</code> is supposed to be child of <code>{post}</code>. But when I hit <code>/posts/1/comments/2</code>, it retrieves <strong>comment</strong> with an id of <strong>2</strong> which belongs to the <strong>post</strong> with an id of <strong>2</strong>. The expected result would be <code>NotFoundHttpException</code>.</p>
  <p>It works fine when I define the routes individually like this:</p>
  <pre><code>Route::get('/posts/{post}/comments/{comment:id}', 'CommentController@show');
  </code></pre>
  <p>Why is this happening?</p>
  <p>Also tried to customize the default key name in both <code>Post</code> and <code>Comment</code> models:</p>
  <pre><code>public function getRouteKeyName()
  {
      return 'id';
  }
  </code></pre>
  <p>But no luck.</p>
  <p>Any help would be appreciated.</p>