【问题标题】:Laravel: Send default value to path in web.phpLaravel:将默认值发送到 web.php 中的路径
【发布时间】:2020-08-30 04:49:00
【问题描述】:

下午好。

我希望在启动我的应用程序时,通向仪表板的索引路径接收默认值“月”,以便用户看到的第一件事是按月过滤的信息。已经在仪表板中,我有一个选择器,它将生成相应的过滤器,但我不知道这是否会影响已经使用默认值定义的路由。

在 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>

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

谢谢。

【问题讨论】:

标签: php laravel eloquent


【解决方案1】:

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

路线:

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

控制器方法:

public function index($filterDashboardMerchant = 'month')

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 2017-05-31
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多