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 GitHub link is not working
Sorry, sometimes I forget to put repositories public. Fixed now!
the main problem here is if backend breakes or network go donw we still have positive info about "delete success" even the item is never deleated
Indeed. That's a separate case that should be handled. For example, if the API did not work - the success message and item state should not change.
You could use one or more Event Callbacks from Inertia.js and make small changes to
deleteTask. This is one possible solution:How to test this code? On the development machine:
Thanks, I updated the article with this!
Either there is a new Sonner version or you may have forgotten to add one line into the file resources/js/layouts/app/AppHeaderLayout.vue. That line is needed for styling, including the positioning of the component. So, the code will be:
Source: Sonner; Installation; then #2.
I'm pretty sure that there was an update with the package, since in the demo code (written for the course) - everything works as expected (including styling)
Updated the lesson with this :)
We originally had
1.3version ofvue-sonner, and they released2.0with new structure.Do I need some sort of a package installed to use roure()?
router.delete(route('tasks.destroy', { id }));
It should be working out of the box, unless they released some new update to the starter kits
Yeah seems they did. I had to install Ziggy myself..
Now Starter Kits have wayfinder installed.
Usage:
CLI:
resources/js/pages/Tasks/Index.vue: