简体   繁体   English

如何修复 Laravel 5.7 中的 MethodNotAllowedHttpException 错误?

[英]How to fix MethodNotAllowedHttpException error in Laravel 5.7?

I added a CRUD interface for my user's table, and instead of a delete button, I used a block button.我为我的用户表添加了一个 CRUD 界面,而不是删除按钮,我使用了阻止按钮。 Which blocks a user (sets bloque field in the database from 0 to 1).阻止用户(将数据库中的 bloque 字段从 0 设置为 1)。 I added a new function in my controller called block which is supposed to do the job yet I get a MethodNotAllowedHttpException error every time I click the blocking button.我在我的控制器中添加了一个名为 block 的新函数,它应该可以完成这项工作,但每次单击阻止按钮时我都会收到MethodNotAllowedHttpException错误。

UserController用户控制器

public function block($id)
{
    $user = User::find($id);
    $user->bloque = 1;
    $user->save();

    return redirect('/users')->with('success', 'Utilisateur bloqué');
}

The blocking HTML fragment阻塞的 HTML 片段

<form action="{{ route('users.block', $user->id)}}" method="get">
@csrf
    <!--  @method('DELETE')-->
    <button class="btn btn-danger" type="submit">Bloquer</button>
</form>

Routes路线

Route::get('/block', [
    'uses' => 'UserController@block',
    'as' => 'users.block'
]);

I think the problem is related to id value, It should be instantiated from $request object.我认为问题与 id 值有关,它应该从 $request 对象实例化。 Like:喜欢:

public function block(Request $request)
{
    $user = User::find($request->id);
    $user->bloque = 1;
    $user->save();

    return redirect('/users')->with('success', 'Utilisateur bloqué');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM