0
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?

More than 3 years have passed since last update.

laravelでルートにnameをつけて、 ルートをnameで呼び出す方法

Posted at

#ルートにnameをつける方法

Route::get('/home', [HomeController::class, 'index'])->name('home');

#ルートをnameで呼び出す方法

route('home')

##引数が欲しい場合
実際laravelで使う場合はこんな感じになる
詳細画面の例でルートを指定するとこうなる

web.php
Route::get('/post/{post}', [PostController::class, 'show'])->name('show');

呼び出す方法はこうなる

show.blade.php
<a href="{{route('show',['post'=>$id])}}">ホーム</a>

コントローラーに引数を書くことを忘れないように注意が必要です

PostController.php
public functin show($post){
}

##補足
resourcesを使ってパスを書いた場合は自動的に名前がつけられているのでphp artisan route:listでnameを確認しましょう

0
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
0
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?