首页 > 解决方案 > 如何在重定向 URL Laravel 8 和 Inertiajs 中保留哈希

问题描述

我正在使用带有 Inertiajs 和 React 的 Laravel 8。我正在尝试重定向到在 url 末尾带有哈希的 url,并且由于某种原因,无论我如何构造 url,Laravel 都会在最后剥离片段。即使我明确使用withFragment,它也不起作用。这是我的控制器:

    /**
     * Handle request to store a new answer
     */
    public function store(Request $request)
    {
        $userId = Auth::id();
        $question = Question::find($request->input('question')['id']);
        $questionAuthor = User::find($question->user_id);
        $answer = $request->input('answer');

        $newAnswer = new Answer; 
        $newAnswer->body = $answer['body'];
        $newAnswer->user_id = $userId;
        $newAnswer->question_id = $question->id;
        $newAnswer->save();

        Mail::to($questionAuthor)->send(new NewAnswer(
            $questionAuthor, 
            $newAnswer,
            $question
        ));

        $url = url("/questions/{$question->slug}");

        return redirect($url)->withFragment($newAnswer->id);
    }

如果有人可以帮助我理解为什么哈希会从最后被剥离以及我如何修复它,我将非常感激。谢谢。

标签: phplaravelredirectlaravel-8inertiajs

解决方案


推荐阅读