我有 2 個物體,稱為Match和Roster。我的比賽路線是這樣的
http://localhost:8888/app/public/matches(索引)
http://localhost:8888/app/public/matches/14(顯示)
為了查看/創建每場特定比賽的球隊,我添加了比賽名單的路線,如下所示:
Route::get('/matches/'.'{id}'.'/roster/', [App\Http\Controllers\RosterController::class, 'index']);
現在我需要我的 URL 中的 {id} 將其傳遞給這里的控制器:
public function index()
{
return view('roster.index');
}
我需要它來做幾件事。首先,我需要通過具有該值的列過濾名冊表進行搜索,這樣我就可以只顯示屬于該比賽的球員。
其次,我需要將它傳遞給視圖,以便我可以在我的商店中使用它并更新表單。我想從同一個索引視圖中添加或洗掉名冊中的球員。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
#1 您可以通過request()->route('parameter_name').
public function index()
{
// get {id} from the route (/matches/{id}/roster)
$id = request()->route('id');
}
#2 您可以通過 using 傳遞資料物件 return view(file_name, object)
public function index()
{
// get {id} from the route (/matches/{id}/roster)
$id = request()->route('id');
// query what u want to show
// dunno ur models specific things, so just simple example.
$rosters = Roster::where('match_id', '=', $id);
// return view & data
return view('roster.index', $rosters);
}
#3 不僅可以做索引,還可以做其他事情(創建、存盤、編輯、更新)
另外,強烈建議先用簡單的例子學習官方教程。像博客、Board 等。你需要了解構建 Laravel 應用程式的基本知識。
uj5u.com熱心網友回復:
大多數時候,我更喜歡命名路線。
Route::get('{bundle}/edit', [BundleController::class, 'edit'])->name('bundle.edit');
在控制器中
public function edit(Bundle $bundle): Response
{
// do your magic here
}
您可以通過以下方式呼叫路線,
route('bundle.edit', $bundle);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411089.html
標籤:
