为什么不在Laravel中删除按钮?

时间:2017-12-16 16:34:55

标签: php laravel-5

我的laravel应用程序中有任务删除按钮,

这是我的刀片文件删除按钮

R CMD check results
0 errors | 0 warnings | 0 notes

这是TaskController删除功能

<a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/delete" class="editInline"><i class="glyphicon glyphicon-trash"></i></a>

这是路线

public function deleteOneProjectTask($projectId, $taskId)
    {
        DB::table('tasks')
            ->where('project_id', $projectId)
            ->where('id', $taskId)
            ->delete();
 return redirect()->route('projects.show')->with('info', 'Task deleted successfully');
    }

但是当我去删除以下错误时,

Route::delete('projects/{projects}/tasks/{tasks}/delete', [
    'uses' => '\App\Http\Controllers\TasksController@deleteOneProjectTask',
]);

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

通过锚点调用网址

<a href="/projects/{{ $project->id }}/tasks/{{ $task->id }}/delete" class="editInline"><i class="glyphicon glyphicon-trash"></i></a>
当你为get做出路线时,

实际上是由DELETE http动词而不是delete,所以你需要为get代替delete而不是{/ 1}} p>

Route::get('projects/{projects}/tasks/{tasks}/delete', [
    'uses' => '\App\Http\Controllers\TasksController@deleteOneProjectTask',
]);

或者您必须在delete的表单标记或method='DELETE'中调用ajax http动词,您可以在其中将http动词设为DELETE

要制作DELETE http动词,请使用form类型按钮submitRead more..

<form action="/projects/{{ $project->id }}/tasks/{{ $task->id }}/delete" method="DELETE">
    <button type="submit"><i class="glyphicon glyphicon-trash"></i></button>
</form>

另请查看this answer以通过ajax调用http动词DELETE。要调用ajax,请使用onclick事件在url中传递相关的ID