Reputation: 71
I have a delete link that links to destroy button
<i class="icon-trash"></i>
<a style="color:black" href="route{{ action('TasksController@destroy', ['id' => $task->id ,'method'=>'DELETE'] ) }}">
delete
</a>
This is the destroy function
public function destroy($id)
{
//delete task
$task = Task::find($id);
$task->delete();
return redirect('/home')->with('success', 'Task deleted successfully');
}
but the link is not working when clicked
Upvotes: 2
Views: 15184
Reputation: 11034
The Blade
syntax is incorrect, change it to:
<i class="icon-trash"></i>
<a style="color:black" href="{{ route('tasks.destroy', ['id' => $task->id]) }}">
delete
</a>
Which requires a route like this:
Route::get('/tasks/delete/{id}', 'TasksController@destroy')
->name('tasks.destroy');
And a controller method like this:
public function destroy($id)
{
// delete task
$task=Task::find($id);
$task->delete();
return redirect('/home')->with('success','Task deleted successfully');
}
Having a get method to delete is a security vulnerability as anyone can trick other users into deleting tasks just by providing a link
Here are some suggestions to improve your code
Change your HTML to perform a post request:
<i class="icon-trash"></i>
<a style="color:black"
href="{{ route('tasks.destroy', ['task' => $task]) }}"
onclick="event.preventDefault();
document.getElementById('delete-form-{{ $task->id }}').submit();">
delete
</a>
<form id="delete-form-{{ $task->id }}" action="{{ route('tasks.destroy', ['task' => $task]) }}"
method="POST" style="display: none;">
@csrf
</form>
And the route to DELETE
with route model binding:
Route::delete('/tasks/delete/{task}', 'TasksController@destroy')
->name('tasks.destroy');
You can add dependency injection to the controller method like this:
public function destroy(Task $task)
{
// delete task
$task->delete();
return redirect('/home')->with('success','Task deleted successfully');
}
Upvotes: 6
Reputation: 3689
you can try this: (you can pass your id)
<form action="{{ route('tasks.destroy', $dummy->id) }}" method="post">
@csrf
@method('DELETE')
<a href="#" class="btn btn-danger" title="Delete" data-toggle="tooltip" onclick="this.closest('form').submit();return false;">
<i class="bi bi-trash-fill" style="color:white"></i>
</a>
</form>
requires route like:
Route::get('/tasks/delete/{id}', 'TasksController@destroy')
->name('tasks.destroy');
your controller:
public function destroy($id)
{
$task = Task::find($id);
$task->delete();
return redirect('/home')->with('success','Task Deleted Successfully');
}
or you can try this
{!! Form::open(['method' => 'DELETE','route' => ['reports.destroy', $dummy->id],'class'=>'']) !!}
{{ Form::button('<i class="bi bi-trash-fill" style="color:white"></i>', ['type' => 'submit', 'class' => 'delete get-started-btn-two'] ) }}
{!! Form::close() !!}
Upvotes: 3
Reputation: 6233
Anchor tags are for GET
requests. You can't use this for POST
or DELETE
. If you want to delete using an anchor tag you have to use a GET
request which is not recommended for deleting an entry. Though it will be something like below
<a href="{{ route('task.destroy',$task->id) }}">
Delete
</a>
Set your route like
Route::get('task/delete/{id}', 'TasksController@destroy')
->name('task.destroy');
And your current controller code will do just fine.
public function destroy($id)
{
$task = Task::find($id);
$task->delete();
return redirect('/home')->with('success','Task Deleted Successfully');
}
Well now lets do it with DELETE
method.
Route
Route::delete('task/delete/{id}', 'TasksController@destroy')
->name('task.destroy');
Send the delete request using a form
<form action="{{ route('task.destroy', $task->id) }}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger btn-sm" title="Delete">Delete</button>
</form>
You can do it using a POST
method too.
Route::post('task/delete/{id}', 'TasksController@destroy')
->name('task.destroy');
<form action="{{ route('task.destroy', $task->id) }}" method="POST">
@csrf
<button class="btn btn-danger btn-sm" title="Delete">Delete</button>
</form>
Your controller code will remain same. My recommendation is either use POST
or DELETE
method to destroy an item from database.
Upvotes: 3