1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】ルーティングの順番と動的パラメータ【簡易メモ】

1
Last updated at Posted at 2025-05-29

背景:一括削除がエラーになる

一括で削除する機能を追加しようと思ったけど、こう書いたらエラーになった。

routes/web.php
Route::resource('articles', ArticlesController::class);
Route::delete('/articles/bulk-delete', [ArticlesController::class, 'bulkDelete'])->name('articles.bulkDelete');

エラー内容は以下。

App\Http\Controllers\ArticlesController::destroy(): Argument #1 ($id) must be of type int, string given

destroyメソッドが呼ばれてる。

原因

/articles/bulk-delete が
/articles/{article} に対して、DELETEリクエストが来たと認識していた!

ルート情報は上から順番に処理される。

対処法

  • Route::resource() より前に、個別のルートを定義する
  • 動的パラメータ(/{id} みたいなもの)があるルートは、後ろに書く
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?