【问题标题】:Redirect to saving post after the send to DB LARAVEL 8发送到 DB LARAVEL 8 后重定向到保存帖子
【发布时间】:2021-05-04 04:18:11
【问题描述】:

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

谢谢

这不起作用

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

控制器

    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();
}

【问题讨论】:

    标签: php laravel controller laravel-8 laravel-controller


    【解决方案1】:

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

    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'
            ]);
    

    【讨论】:

      猜你喜欢
      • 2016-06-30
      • 2019-12-15
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多