在本系列的早期部分中,您更新了现有的 Artisan 命令以支持新列表功能. 虽然有插入和删除链接的命令,但演示应用程序目前没有编辑现有链接的命令。
在本指南中,您将创建一个新的 Artisan 命令来更新数据库中的现有链接。
从您的终端,首先确保您在项目的根目录中,然后运行以下操作来启动新的 Artisan 命令:
1docker-compose exec app php artisan make:command LinkUpdate
这将创建一个新的LinkUpdate.php
文件,位于app/Console/Commands
。
1app/Console/Commands/LinkUpdate.php
此文件包含一个新的 Artisan 命令的 boilerplate 代码. 您将更新它以处理编辑链接,只要其独特的 id。
- 获取用户提供的 id,并检查数据库中是否存在具有匹配 id的链接。
- 如果无法找到有效链接,请显示错误消息并退出。
首先,在文件的顶部包含几个使用
定义,以便稍后更容易引用链接
和链接列表
类:
1[label app/Console/Commands/LinkUpdate.php]
2<?php
3
4namespace App\Console\Commands;
5
6use App\Models\Link;
7use App\Models\LinkList;
8use Illuminate\Console\Command;
9
10...
要获取链接 id,您应该在新的link:update
命令中设置一个强制性的参数,以便用户在运行时提供该参数。
1[label app/Console/Commands/LinkUpdate.php]
2...
3
4class LinkUpdate extends Command
5{
6 /**
7 * The name and signature of the console command.
8 *
9 * @var string
10 */
11 protected $signature = 'link:update {link_id}';
12...
如果您保存该文件并尝试现在运行命令,而无需额外的参数,则会收到错误:
1docker-compose exec app php artisan link:update
1[secondary_label Output]
2Not enough arguments (missing: "link_id").
在handle()
方法中,您需要获取用户提供的链接 id并将其定位在数据库中。这可以通过母类Command
提供的argument()
方法来完成。然后,您可以使用find()
Eloquent 方法来查询数据库中与该 id的链接。如果find()
方法返回null
,则意味着没有与该 id的链接被找到,因此程序应该错误地退出。
1[label app/Console/Commands/LinkUpdate.php]
2...
3 /**
4 * Execute the console command.
5 *
6 * @return int
7 */
8 public function handle()
9 {
10 $link_id = $this->argument('link_id');
11 $link = Link::find($link_id);
12
13 if ($link === null) {
14 $this->error("Invalid or non-existent link ID.");
15 return 1;
16 }
17
18 // obtain updated information from user
19 }
20...
当找到有效的链接时,您需要请用户查看更新的链接信息,您可以使用以下示例中突出的 ask
方法来完成此操作:
1[label app/Console/Commands/LinkUpdate.php: function handle() ]
2...
3 if ($link === null) {
4 $this->error("Invalid or non-existent link ID.");
5 return 1;
6 }
7
8 $link->description = $this->ask('Link Description (ENTER to keep current)') ?? $link->description;
9 $list_name = $this->ask('Link List (ENTER to keep current)') ?? $link->link_list->title;
10...
此代码将提示用户更新描述和列表,同时将当前值保留为默认值,如果用户不提供新值,请按ENTER
跳过提示。
一旦你有所有这些信息,你可以继续更新,这是一个好主意,使用‘confirm()’方法,让用户在运行数据库更新之前确认更改。
1[label app/Console/Commands/LinkUpdate.php: function handle() ]
2...
3 $link->description = $this->ask('Link Description (ENTER to keep current)') ?? $link->description;
4 $list_name = $this->ask('Link List (ENTER to keep current)') ?? $link->link_list->title;
5
6 $this->info("Description: $link->description");
7 $this->info("Listed in: " . $list_name);
8
9 if ($this->confirm('Is this information correct?')) {
10 //code that updates the link
11 }
12...
在如果
区块中,你必须首先检查是否有所请求的列表,否则创建一个新的列表以提供的名称。然后,你会使用associate()
方法来更新这个链接与其家长
列表之间的关系。
1[label app/Console/Commands/LinkUpdate.php: function handle() ]
2...
3 if ($this->confirm('Is this information correct?')) {
4 $list = LinkList::firstWhere('slug', $list_name);
5 if (!$list) {
6 $list = new LinkList();
7 $list->title = $list_name;
8 $list->slug = $list_name;
9 $list->save();
10 }
11 $link->link_list()->associate($list)->save();
12 $this->info("Updated.");
13 }
14...
这里是您的参考完整的LinkUpdate.php
文件:
1[label app/Console/Commands/LinkUpdate.php]
2<?php
3
4namespace App\Console\Commands;
5
6use App\Models\Link;
7use App\Models\LinkList;
8use Illuminate\Console\Command;
9
10class LinkUpdate extends Command
11{
12 /**
13 * The name and signature of the console command.
14 *
15 * @var string
16 */
17 protected $signature = 'link:update {link_id}';
18
19 /**
20 * The console command description.
21 *
22 * @var string
23 */
24 protected $description = 'Update a link in the database';
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 $link_id = $this->argument('link_id');
44 $link = Link::find($link_id);
45
46 if ($link === null) {
47 $this->error("Invalid or non-existent link ID.");
48 return 1;
49 }
50
51 $link->description = $this->ask('Link Description (ENTER to keep current)') ?? $link->description;
52 $list_name = $this->ask('Link List (ENTER to keep current)') ?? $link->link_list->title;
53
54 $this->info("Description: $link->description");
55 $this->info("Listed in: " . $list_name);
56
57 if ($this->confirm('Is this information correct?')) {
58 $list = LinkList::firstWhere('slug', $list_name);
59 if (!$list) {
60 $list = new LinkList();
61 $list->title = $list_name;
62 $list->slug = $list_name;
63 $list->save();
64 }
65 $link->link_list()->associate($list)->save();
66 $this->info("Updated.");
67 }
68
69 return 0;
70 }
71}
<$>[注] 注: 有关艺术家命令的更多详细信息,请参阅我们在Laravel中如何创建艺术家命令来管理数据库记录(How To Create Artisan Commands to Manage Database Records in Laravel)的指南(https://andsky.com/tech/tutorials/how-to-create-artisan-commands-to-manage-database-records-in-laravel),这是我们介绍Laravel系列的一部分。
完成后保存文件,然后使用link:show
命令获取所有链接及其相应的ID:
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 | 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/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 网站的链接创建一个 digitalocean列表(在上一个示例输出中,这对应于具有 ID 的项目 1, 2和 3)。
若要用 ID 1 更新链接,请运行:
1docker-compose exec app php artisan link:update 1
1[secondary_label Output]
2 Link Description (ENTER to keep current):
3 > DO Community
4
5 Link List (ENTER to keep current):
6 > digitalocean
7
8Description: DO Community
9Listed in: digitalocean
10
11 Is this information correct? (yes/no) [no]:
12 > y
13
14Updated.
然后,再次运行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+----+-------------------------------------------------+--------------+----------------------------------+
在本指南中,您了解了如何使用 Laravel Eloquent 更新数据库记录. 您升级了演示应用程序,以包括一个新的命令,允许用户编辑数据库中现有链接。
在本系列的下一部分和最后一部分,您将创建一个新的命令来删除链接列表。