我们到了最后一个方法:destroy
。您猜对了,该destroy
方法将从数据库中删除资源。这是最简单的一堆。我们只需要创建一个链接,删除一个资源并将用户返回到页面index
。
路由
路线已经创建。我们在几篇文章之前就这样做了。
Route::prefix('/personalcars')->group(function() {
Route::delete('/{id}', [PersonalCarController::class, 'destroy']);
});
delete
我们只需要向我们的路线发送请求personalcars/{id}
。
创建删除请求
在我们销毁资源之前,我们需要能够到达那里。你如何delete
向 Laravel 发送请求?用表格。
<form method="post" action="...">
@csrf
@method('delete')
<button type="submit">Delete</button>
</form>
我们只需要将我们的按钮添加到我们认为资源应该存在的位置。我会把它添加到我们的index.blade.php
文件旁边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
到PersonalCarController
. 在其中,我们只需要删除资源并将用户重定向回index.blade.php
成功消息即可。
<?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.');
}
}
我经历并删除了一堆汽车。现在只有一个问题。我们关于那辆车的图像仍然位于关联的表格中。我们将在下一篇文章中进行图像清理。
mysql> select * from image_personal_car;标签:Laravel,01,22,29,Chevy,2023,P46,id,CMP From: https://blog.51cto.com/u_1213352/6052180
+-----------------+----------+
| 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)