php - Laravel:将默认值发送到 web.php 中的路径
问题描述
下午好。
我希望在启动我的应用程序时,指向仪表板的索引路径接收默认值“月”,以便用户看到的第一件事是按月过滤的信息。已经在仪表板中,我有一个选择器,它将生成相应的过滤器,但我不知道这是否会影响已经使用默认值定义的路由。
在 web.php 我有:
Route::get('merchant-dashboard/{filterDashboardMerchant}', 'HomeController@index')->name('merchant-dashboard')->middleware(['auth.shopify']);
$filterDashboardMerchant 是我用来过滤要显示的信息的变量,它获取 'today'、'month' 和 'year' 之间的值。
在控制器中,我收到它是这样的:
public function index($filterDashboardMerchant)
{
.............
}
仪表板视图中的过滤器是以下选择:
<select id="filter-records" class="custom-select-design1">
<option value="{{ route('merchant-dashboard', $filterDashboardMerchant = 'today') }}" {{$filterDashboardMerchant==='today' ? 'selected="selected"' : ''}}>Today</option>
<option value="{{ route('merchant-dashboard', $filterDashboardMerchant = 'month') }}" {{$filterDashboardMerchant==='month' ? 'selected="selected"' : ''}}>Month</option>
<option value="{{ route('merchant-dashboard', $filterDashboardMerchant = 'year') }}" {{$filterDashboardMerchant==='year' ? 'selected="selected"' : ''}}>Year</option>
</select>
也许这是语法问题,但我尝试了几个选项,但没有一个对我有用。我希望你能帮助我。
谢谢你。
解决方案
一种选择是使参数可选,并在控制器中设置其默认值。
路线:
Route::get('merchant-dashboard/{filterDashboardMerchant?}', 'HomeController@index')->name('merchant-dashboard')->middleware(['auth.shopify']);
控制器方法:
public function index($filterDashboardMerchant = 'month')
推荐阅读
- inno-setup - 在 Inno Setup 脚本中使用在 Inno Setup 编译器命令行上指定的路径/值
- python - Python:如何摆脱嵌套循环?
- testing - 如何使用单个命令运行多个测试类?
- javascript - Vue计算不会每次都计算
- elasticsearch - 获取子文档
- python-3.x - 有没有办法读取随机打印的?
- reactjs - docker-compose up 卡在附加到
- typescript - 仅将 lodash 中的“get”函数导入打字稿
- .net-core - 有没有办法将不同命名的环境变量映射到 IOptions
实现模式? - javascript - 为什么我的三元无法测试数组的元素?