In this lesson, let's add the Delete button to our table.
Delete Button
Here's what we have in the Controller:
app/Http/Controllers/TaskController.php:
public function destroy(Task $task){ $task->delete(); return redirect()->route('tasks.index');}
Since we have Route::resource()
here, the Vue component should fire a DELETE request to the route
/tasks/{ID}
.
So, first, we define the function inside the Vue component.
resources/js/pages/Tasks/Index.vue:
import { Head } from '@inertiajs/vue3'; import { Head, router } from '@inertiajs/vue3'; // ... defineProps<Props>(); const deleteTask = (id: number) => { if (confirm('Are you sure you want to delete this task?')) { router.delete(route('tasks.destroy', { id })); }}; // ...
Then, we need to add a Button to the table that calls the deleteTask()
method.
The Shadcn Button component is already installed in the starter kit, so we don't need to run any npx
commands. We just need to import it with button variants.
We add a new <TableCell>
to...