Laravel 8 从控制器向 return 视图添加 GET 参数

Laravel 8 add GET parameters to return view from controller

我真的是 Laravel 的新手:)

在 laravel 8 控制器中,有一种方法可以向视图添加额外的 GET 参数 return?

在 web.php 我有:

Route::get('/listtestmodel', 'TestmodelController@list')->name('listtestmodel');

我在控制器中(在列表函数中):

 return view('page.list', ['list' => $list]);

$list 是一个变量,其中包含要在视图中呈现的模型集合。

我想向页面添加 url 一个新参数。 现在我

/listtestmodel

我需要类似的东西

/listtestmodel?par=345

如果您只需要在生成 url 时向查询字符串添加参数,您可以简单地这样做:

// This will output the url with the query string
route('listtestmodel', ['par' => 345]);

然后在你的控制器中你可以通过请求助手获取参数

request()->input('par');

或者,如果您可以接受 url /listtestmodel/345,您可以更改路线并以这种方式获取参数:

// Route in web.php
Route::get('/listtestmodel/{par}', 'TestmodelController@list')->name('listtestmodel');


// Method in controller
public function list($par)
{
...
}

如果您的参数是模型 ID,则应使用第二种方法并使用 Laravel 提供的路由模型绑定: https://laravel.com/docs/8.x/routing#route-model-binding