【问题标题】:Why do my routes fail after calling a destroy function?为什么我的路由在调用破坏函数后会失败?
【发布时间】:2021-12-22 13:41:04
【问题描述】:

这段代码已经进行了几次迭代,但我只会展示迄今为止效果最好的代码(没有错误)。

销毁函数:

public function destroy($locale, $id)
{
    Component::where('id', $id)->delete();

    $locale = App::getLocale();

    return route('components.index', compact('locale'));
}

索引函数(参考上面):

public function index($locale)
{
    parent::lang($locale);

    $components = Component::paginate(10);

    return view('production/index-component', compact('components'));
}

相关路线:

Route::group(['prefix' => '{locale}'], function() {
    Route::resource('/components', ComponentController::class);
});

Resulting URL

否则,销毁功能会起作用,记录将被删除(在转到正确的 URL 时)。理想情况下,这应该只是将用户重定向回 {locale}/components。

提前谢谢你!

【问题讨论】:

标签: php laravel web eloquent routes


【解决方案1】:

归功于 lagbox,

public function destroy($locale, $id)
{

    Component::where('id', $id)->delete();

    $locale = App::getLocale();

    return redirect()->route('components.index', ['locale' => $locale]);
}

效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    相关资源
    最近更新 更多