Laravel App的删除表格

时间:2016-02-22 18:47:16

标签: php laravel laravel-5.2

当我点击Laravel应用程序中的删除按钮时,我不确定为什么我收到NotFoundHTTPException。

index.blade.php

<form action="/users/3" method="POST">
    <input type="hidden" value="DELETE" name="_method">
    <button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>

UsersController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Gate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UsersController extends Controller
{
    public function destroy()
    {
        return 'yes';
    }
}

routes.php文件

Route::get('users', ['as' => 'users', 'uses' => 'UsersController@index']);
Route::get('users/{user}', ['as' => 'users.show', 'uses' => 'UsersController@show']);
Route::get('users/{user}/edit', ['as' => 'users.edit', 'uses' => 'UsersController@edit']);
Route::delete('users/{user}', ['as' => 'users.delete', 'uses' => 'UsersController@destroy']);

3 个答案:

答案 0 :(得分:2)

更改表格:

<form action="/users/3" method="DELETE">
    <button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>

您正在使用POST并且尚未为“DELETE”动词定义路线。定义帖子而不是删除路由(Route :: post(...))或将表单更改为用户删除方法,如上所述。

答案 1 :(得分:1)

你应该改变:

<form action="/users/3" method="POST">

以下内容:

<form action="<?= route('users.delete', [$user]) ?>" method="POST">

答案 2 :(得分:1)

index.blade.php

<form action="{{url('/users/3')}}" method="DELETE">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>

UsersController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Gate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UsersController extends Controller
{
    public function destroy($id)
    {
        return 'yes';
    }
}