Simplifying Data Removal with Laravel’s Controller Destroy Method
We’re down to our last method: destroy
. The destroy
method will, you guessed it, remove a resource from the database. This is the simplest of the bunch. We simply need to create a link, delete a resource and return the user to the index
page.
https://medium.com/geekculture/laravel-p45-controller-edit-update-cmp-b16b5b77aaaa
Route
The route is already created. We did this a few articles ago.
Route::prefix('/personalcars')->group(function() {
Route::delete('/{id}', [PersonalCarController::class, 'destroy']);
});
We simply need to send a delete
request to our personalcars/{id}
route.
Creating a delete request
Before we can destroy a resource, we need to be able to get there. How do you send a delete
request to Laravel? With a form.
<form method="post" action="...">
@csrf
@method('delete')
<button type="submit">Delete</button>
</form>
We simply need to add our button to where we believe the resource should exist. I’ll add it to our index.blade.php
file next to Edit Car
.
<x-layouts.app title="{{ $title }}">
<div class="flex bg-white mt-12">
<div class="items-center text-center lg:text-left px-8 md:px-12 lg:w-full">
<div class="relative overflow-x-auto">
@if (session('status'))
<div class="block bg-green-200 p-4">
{{ session('status') }}
</div>
@endif
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<!-- ... -->
<th scope="col" class="px-6 py-3">
Delete
</th>
</tr>
</thead>
<tbody>
@foreach($cars as $car)
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<!-- ... -->
<td class="px-6 py-4">
<form method="post" action="/personalcars/{{ $car->id }}">
@csrf
@method('delete')
<button type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
<a href="/personalcars/create">
<button class="block bg-green-400 hover:bg-green-600 text-white uppercase text-lg mx-auto p-4 rounded" type="submit">Add New Car</button>
</a>
</x-layouts.app>
Destroy Method
The route will direct the code to the destroy
method in the PersonalCarController
. Inside of it, we simply need to delete the resource and redirect the user back to index.blade.php
with a success message.
<?php
namespace App\Http\Controllers;
use App\Models\Image;
use App\Models\PersonalCar;
use App\Models\PersonalCarBrand;
use App\Models\PersonalCarModel;
use Faker\Provider\Person;
use Illuminate\Http\Request;
class PersonalCarController extends Controller
{
// ...
/**
* Remove the specified resource from storage.
*
* @param int $id
*/
public function destroy($id)
{
PersonalCar::destroy($id);
return redirect()->to('/personalcars/')->with('status', 'Your car has been deleted.');
}
}
I went through and deleted a bunch of cars. There’s only one problem now. Our images for that car are still located in the associated tables. We’ll do image cleanup in the next article.
mysql> select * from image_personal_car;
+-----------------+----------+
| personal_car_id | image_id |
+-----------------+----------+
| 1 | 1 |
| 1 | 3 |
| 18 | 6 |
| 15 | 7 |
| 15 | 8 |
| 18 | 9 |
+-----------------+----------+
6 rows in set (0.00 sec)
mysql> select * from images;
+----+---------------------------------------------------------------+-------------------------+---------------------+---------------------+
| id | url | alt | created_at | updated_at |
+----+---------------------------------------------------------------+-------------------------+---------------------+---------------------+
| 1 | https://pngimg.com/uploads/chevrolet/%D1%81hevrolet_PNG18.png | 2007 Chevy Corvette | 2023-01-22 13:15:11 | 2023-01-22 13:15:11 |
| 2 | https://pngimg.com/uploads/chevrolet/%D1%81hevrolet_PNG23.png | 2003 Chevy Corvette | 2023-01-22 13:29:50 | 2023-01-22 13:29:50 |
| 3 | https://pngimg.com/uploads/chevrolet/%D1%81hevrolet_PNG25.png | 2003 Chevy Corvette | 2023-01-22 13:30:08 | 2023-01-22 13:30:08 |
| 4 | https://pngimg.com/uploads/chevrolet/%D1%81hevrolet_PNG60.png | Test Image | 2023-01-22 13:36:52 | 2023-01-22 13:36:52 |
| 5 | images/CHl8y3afKXuwgE8DZWt0D61vOwUEkB11CtSIl0lO.jpg | 2003 Chevy Corvette | 2023-01-28 17:46:41 | 2023-01-28 17:46:41 |
| 6 | images/yrqW77FwXqfwmdLA3rH2FQFoavAXxuolz48s5LKu.jpg | 2003 Chevy Corvette | 2023-01-28 17:47:02 | 2023-01-28 17:47:02 |
| 7 | images/ZFfJMUIIu0B8JXjqux51poDcCUTm6hC3msIM9C8n.jpg | 2024 Chevy Corvette Z06 | 2023-01-29 22:07:37 | 2023-01-29 22:07:37 |
| 8 | images/b44fQE1vLjfmEJfJvbIeoMmKNKUjiztUPSByEHJk.jpg | 2024 Chevy Corvette Z06 | 2023-01-29 22:07:43 | 2023-01-29 22:07:43 |
| 9 | images/BoCbU6B71w1e1WuKpLGdIOv6nsVUaiI1NtiQcmJ6.jpg | 2003 Chevy Corvette | 2023-01-29 22:14:08 | 2023-01-29 22:14:08 |
+----+---------------------------------------------------------------+-------------------------+---------------------+---------------------+
9 rows in set (0.00 sec)
https://github.com/dinocajic/youtube-laravel
Laravel Series
Continue your Laravel Learning.
How to Use Laravel’s Controller Edit/Update Methods Effectively
Laravel – P45: CMP – Controller Edit/Update
Learn how to effectively implement Edit and Update methods in Laravel controllers to manage data updates smoothly in your applications.
Simplifying Data Removal with Laravel’s Controller Destroy Method
Laravel – P46: CMP – Controller Destroy
Learn how to use the Destroy method in Laravel controllers to efficiently handle data deletions and maintain clean data management.