Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Delete Task and Shadcn Toast Notification

Premium
4 min read

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 React component should fire a DELETE request to the route /tasks/{ID}.

So, first, we define the function inside the React component.

resources/js/pages/Tasks/Index.tsx:

import { Head } from '@inertiajs/react';
import { Head, router } from '@inertiajs/react';
 
// ...
 
export default function Index({ tasks }: { tasks: Task[] }) {
const deleteTask = (id: number) => {
if (confirm('Are you sure?')) {
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 our <TableRow>.

resources/js/pages/Tasks/Index.tsx:

import { Button, buttonVariants } from '@/components/ui/button';
 
// ...
 
{tasks.map((task) => (
<TableRow key={task.id}>
<TableCell>{task.name}</TableCell>
<TableCell className={task.is_completed ? 'text-green-600' : 'text-red-700'}>
{task.is_completed ? 'Completed' : 'In Progress'}
</TableCell>
<TableCell className="flex flex-row gap-x-2 text-right"> // [tl! add:start]
<Button variant={'destructive'} className={'cursor-pointer'} onClick={() => deleteTask(task.id)}>
Delete
</Button>
</TableCell> // [tl! add:end]
</TableRow>
))}

Here's the visual result:

Now, see that variant={'destructive'}? To see what other button variants are available, look at...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

T
TuanTQ ✓ Link copied!
const deleteTask = (task: Task) => {
        if (confirm('Are you sure?')) {
            router.delete(route('tasks.destroy', { task }));
            toast.success('Task deleted successfully');
        }
    };
<TableCell className="flex flex-row gap-x-2 text-right">
		<Button variant={'destructive'} className={'cursor-pointer'} onClick={() => deleteTask(task)}>
				Delete
		</Button>
</TableCell>

Dear sir, can we can use task instead id ? In route and controller we also use model binding

MS
Mike Scott ✓ Link copied!

Using the model seems more logical than the ID, since it matches the parameter better definitions. Good suggestion :)

AE
Abdullah Elian ✓ Link copied!

route('tasks.destroy', { id }) only works if the Ziggy package is installed. import { route } from 'ziggy-js'; also
you need to add @routes before vite(['resources/js/app.tsx'])

M
MicroDev ✓ Link copied!

IDE Error:

TS2552: Cannot find name 'route'. Did you mean 'router'?

Console Error:

index.tsx:18 Uncaught ReferenceError: route is not defined

Solution

  1. Install Ziggy

    composer require tightenco/ziggy
    
  2. Add the @routes directive Place it before your application's JavaScript in resources/views/app.blade.php:

    @routes
    @viteReactRefresh
    @vite(['resources/js/app.tsx', "resources/js/pages/{$page['component']}.tsx"])
    @inertiaHead
    

    This will make the route() helper function available globally.

  3. (Optional - IDE Awareness for React/Vue components)

    1. Add Ziggy path to your tsconfig.json
       "paths": {
          "@/*": ["./resources/js/*"],
          "ziggy-js": ["./vendor/tightenco/ziggy"]
       }
    
    1. Open resources/js/types/index.d.ts file and add the following:
       import { route as routeFn } from 'ziggy-js';
    
       declare global {
           var route: typeof routeFn;
       }
    

For more information, see the Ziggy documentation.

Hope this helps someone who runs into the same issue :)

R
RS_DEV ✓ Link copied!

Came to comments to add a similar solution I had to go through