编辑用户详细信息后,提交时需要重定向到具有新编辑详细信息的用户页面



我面临着一个简单但困难的路由问题! 所以我正在使用刀片与Laravel一起构建一个应用程序。我的问题很简单,当我编辑用户详细信息时,我会根据需要重定向到我的用户页面,但信息没有更新!我该怎么做?我尝试了很多东西,我再也看不出错了!

有人可以帮助我理解我的错误吗? 谢谢! 一个法国新手:)

<button type="submit" class="btn btn-outline-success btn-block"><a href="{{redirect()->route('users.show',['id'=>$user->id])}}"></a>Valider la modification</button>

<button><a>href属性优先于<form>action属性,因此永远不会调用更新操作。您应该在路由操作中执行重定向,例如控制器:

class UserController extends Controller
{
    // other actions
    public function update(Request $request, $id)
    {
        $user = User::find($id);
        $user->fill($request->all()); // Do not fill unvalidated data
        if (!$user->save()) {
            // Handle error
            // Redirect to the edit form while preserving the input
            return redirect()->back()->withInput();
        }
        // Redirect to the 'show' page on success
        return redirect()->route('users.show', ['id' => $user->id]);
    }
    // more actions
}

然后,您的表单应如下所示:

<form action="{{ route('user.update', ['id' => $user->id]) }}" method="POST">
    <!-- Use @method and @csrf depending on your route's HTTP verb and if you have CSRF protection enabled -->
    @method('PUT')
    @csrf
    <!-- Your form fields -->
    <button type="submit" class="btn btn-outline-success btn-block">
        Valider la modification
    </button>
</form>

相关内容

最新更新