1
votes

I have task delete button in My laravel application,

this is My blade file delete button

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

this is TaskController delete function

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');
    }

and this is route

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

but when I go to delete following error is coming,

MethodNotAllowedHttpException in RouteCollection.php line 218:

how can fix this problem?

1
it is because link is a get request and not a delete requestDhaval Chheda
how to make it as delete request?DNK

1 Answers

2
votes

Calling url by anchor like

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

is actually by get http verb not DELETE as you make route for delete so you need to make it for get instead delete like

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

Or you have to call as delete http verb, either by method='DELETE' in form tag or by ajax where you can make it http verb as DELETE

To make a DELETE http verb, use form with submit type button, Read more..

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

Also take a look this answer to call http verb DELETE by ajax. To call ajax use onclick Event to pass related id's in url