发送到数据库 LARAVEL 8 后重定向到保存 post

Redirect to saving post after the send to DB LARAVEL 8

在将锦标赛保存到我的新锦标赛网站后,我如何编写重定向路由以进行重定向。

谢谢

这行不通

return redirect()->route('tournaments.show', $tournament->slug);

Controller

    public function store(Request $request)
{
    $tournament = new Tournament();
    $tournament->title = $request->title;
    $tournament->city = $request->city;
    $tournament->street = $request->street;
    $tournament->game_room = $request->game_room;
    $tournament->email = $request->email;
    $tournament->registration_link = $request->registration_link;
    $tournament->text = $request->text;
    $tournament->phone = $request->phone;
    $tournament->time_registration_at = $request->time_registration_at;
    $tournament->date_registration_at = $request->date_registration_at;
    $tournament->time_starter_at = $request->time_starter_at;
    $tournament->date_starter_at = $request->date_starter_at;
    $tournament->user_id = Auth::user()->id;
    $tournament->region_id = $request->region_id;
    $tournament->slug = SlugService::createSlug(Tournament::class, 'slug', $request->title);
    $tournament->save();

    return redirect()->back();
}

如果使用资源控制器,这是不正确的,

return redirect()->route('tournaments.show', $tournament->slug);

应该是

return redirect()->route('tournaments.show', ['tournament'=>$tournament->slug]);

假设您的路由模型绑定的 routeKeyName 设置为使用 slug 而不是模型中的 id,或者使用 web.php

中新的 laravel 路由模型绑定
Route::resource('tournaments',TournamentController::class)->parameters([
            'tournament'=>'tournaments:slug'
        ]);