Courses

Vue Laravel 12 Starter Kit: CRUD Project

Delete Task and Shadcn Vue Toast Notification

Summary of this lesson:
- Adding delete functionality to the tasks table
- Using Shadcn Vue Button component with destructive variant
- Implementing confirmation before delete
- Adding success notifications with Sonner toast component

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...

The full lesson is only for Premium Members.
Want to access all 14 lessons of this course? (82 min read)

You also get:

  • 73 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord