如何在 Laravel Eloquent 中删除数据库记录

在Eloquent中,您可以方便地使用删除方法从母级模型类中删除数据库记录。在演示应用程序的基本版本中已经实施的link:delete命令会根据有效的链接删除链接 id

在本系列的最后一部分中,您将创建一个新的命令来删除列表. 为了简化,与要删除的列表相关的任何链接将重新分配到默认链接列表。

从终端运行以下操作来启动新的 Artisan 命令:

1docker-compose exec app php artisan make:command ListDelete

这将创建一个新的ListDelete.php文件,位于app/Console/Commands

1app/Console/Commands/ListDelete.php

您将更新此代码以处理删除链接列表,只要其独特的 slug ,这是用于识别每个列表的 URL 友好的名称。

这就是你的handle()方法需要做的事情:

  • 获取用户提供的 ** slug ,并检查数据库中是否存在具有匹配** slug** 的列表。 * 如果无法找到有效列表,请显示错误消息并退出。

如果你一直在跟踪到目前为止系列的所有部分,你以前在创建)`方法之前,你需要运行大规模更新以更改相关链接到另一个列表。

在您的ListDelete.php文件中取代 boilerplate 代码如下:

 1[label app/Console/Commands/ListDelete.php]
 2<?php
 3
 4namespace App\Console\Commands;
 5
 6use App\Models\Link;
 7use App\Models\LinkList;
 8use Illuminate\Console\Command;
 9
10class ListDelete extends Command
11{
12    /**
13     * The name and signature of the console command.
14     *
15     * @var string
16     */
17    protected $signature = 'list:delete {list_slug}';
18
19    /**
20     * The console command description.
21     *
22     * @var string
23     */
24    protected $description = 'Delete Lists';
25
26    /**
27     * Create a new command instance.
28     *
29     * @return void
30     */
31    public function __construct()
32    {
33        parent::__construct();
34    }
35
36    /**
37     * Execute the console command.
38     *
39     * @return int
40     */
41    public function handle()
42    {
43        $list_slug = $this->argument('list_slug');
44        $list = LinkList::firstWhere('slug', $list_slug);
45
46        if ($list === null) {
47            $this->error("Invalid or non-existent List.");
48            return 1;
49        }
50
51        if ($this->confirm("Confirm deleting the list '$list->title'? Links will be reassigned to the default list.")) {
52            $default_list = LinkList::firstWhere('slug', 'default');
53            if (!$default_list) {
54                $default_list = new LinkList();
55                $default_list->title = 'default';
56                $default_list->slug = 'default';
57                $default_list->save();
58            }
59
60            $this->info("Reassigning links to default list...");
61
62            Link::where('link_list_id', $list->id)->update(['link_list_id' => $default_list->id]);
63
64            $list->delete();
65            $this->info("List Deleted.");
66        }
67
68        return 0;
69    }
70}

保存檔案

在之前的代码中,handle()方法始于试图根据提供的 slug 找到链接列表。如果找不到有效列表,应用程序会错误地退出。

确认后,应用程序将查找默认列表或在必要时创建新的列表,将其分配到$default_list变量。

接下来,它会查找并更新与即将删除的列表相关联的所有链接。将链接调用到)`调用中定义的条件。

最后,列表被删除以使用 delete() 方法,也被突出。 此方法可用于所有 Eloquent 模型通过母类 Model 类。

若要删除列表,请先运行link:show,以获取当前数据库中的所有链接:

1docker-compose exec app php artisan link:show
 1[secondary_label Output]
 2+----+-------------------------------------------------+--------------+----------------------------------+
 3| id | url                                             | list         | description                      |
 4+----+-------------------------------------------------+--------------+----------------------------------+
 5| 1  | https://digitalocean.com/community              | digitalocean | DO Community                     |
 6| 2  | https://digitalocean.com/community/tags/laravel | digitalocean | Laravel Tutorias at DigitalOcean |
 7| 3  | https://digitalocean.com/community/tags/php     | digitalocean | PHP Tutorials at DigitalOcean    |
 8| 4  | https://twitter.com/digitalocean                | social       | Twitter                          |
 9| 5  | https://dev.to/digitalocean                     | social       | DEV.to                           |
10| 6  | https://laravel.com/docs/8.x/eloquent           | default      | Laravel Eloquent Docs            |
11+----+-------------------------------------------------+--------------+----------------------------------+

要删除 digitalocean 列表并将这些链接返回** 默认** 列表,运行:

1docker-compose exec app php artisan list:delete digitalocean

通过键入y并点击ENTER来确认删除。

1[secondary_label Output]
2 Confirm deleting the list 'digitalocean'? Links will be reassigned to the default list. (yes/no) [no]:
3 > y
4
5Reassigning links to default list...
6List Deleted.

如果您再次运行link:show()命令,您将看到更新的信息:

 1[secondary_label Output]
 2+----+-------------------------------------------------+---------+----------------------------------+
 3| id | url                                             | list    | description                      |
 4+----+-------------------------------------------------+---------+----------------------------------+
 5| 1  | https://digitalocean.com/community              | default | DO Community                     |
 6| 2  | https://digitalocean.com/community/tags/laravel | default | Laravel Tutorias at DigitalOcean |
 7| 3  | https://digitalocean.com/community/tags/php     | default | PHP Tutorials at DigitalOcean    |
 8| 4  | https://twitter.com/erikaheidi                  | social  | Twitter                          |
 9| 5  | https://dev.to/erikaheidi                       | social  | DEV.to                           |
10| 6  | https://laravel.com/docs/8.x/eloquent           | default | Laravel Eloquent Docs            |
11+----+-------------------------------------------------+---------+----------------------------------+

该应用程序现在有一个专用命令来删除链接列表。

Published At
Categories with 技术
comments powered by Disqus