首页 > 解决方案 > 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>

也许这是语法问题,但我尝试了几个选项,但没有一个对我有用。我希望你能帮助我。

谢谢你。

标签: phplaraveleloquent

解决方案


一种选择是使参数可选,并在控制器中设置其默认值。

路线:

Route::get('merchant-dashboard/{filterDashboardMerchant?}', 'HomeController@index')->name('merchant-dashboard')->middleware(['auth.shopify']);

控制器方法:

public function index($filterDashboardMerchant = 'month')

推荐阅读