6

How To Create Artisan Commands To Manage Database Records in Laravel

 2 years ago
source link: https://www.digitalocean.com/community/tutorials/how-to-create-artisan-commands-to-manage-database-records-in-laravel
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

// Tutorial //

How To Create Artisan Commands To Manage Database Records in Laravel

Published on December 17, 2020 · Updated on April 29, 2022
By Erika Heidi
Developer Advocate
How To Create Artisan Commands To Manage Database Records in Laravel

If you followed along with this series so far, your database tables should be all set. However, you still need to implement a way to let users insert new entries in the links table.

To limit the scope of this series while also making the application fully-functional, you’ll set up Artisan commands to create and delete links in the database. Artisan is the command line tool that comes with Laravel, offering a number of utilities to speed up the development process, from generating boilerplate code to deleting and re-creating the application’s database.

Using the command line interface to manage your application can be an alternative to web forms and secured areas, since it requires a user to be logged on the server in order to execute such commands instead of being authenticated from a browser. If you decide later on to create a secured area for your application, you can create web forms to allow a registered user to submit a new link to the database.

Artisan commands are often used to perform application tasks that should run in the background, either manually or automatically via a scheduling mechanism such as Crontab. They can also be used to facilitate prototyping new application features that need to be configured dynamically, depending on input from an authorized user.

To get started, create a new Artisan command using the make:command helper:

docker-compose exec app php artisan make:command LinkNew
Output
Console command created successfully.

This will create a file named LinkNew.php, located at the app/Console/Commands directory. In this class, which extends from the Illuminate\Console\Command parent class, you’ll need to implement a handle method that will be executed when this command is called. To define the signature of the command, you’ll set the $signature protected property to link:new.

Open the new file using your text or code editor of choice. Here, nano is used:

nano app/Console/Commands/LinkNew.php

A few different things will need to happen in the handle method so that you are able to save a new link to the database. First, you’ll prompt for the user’s input in order to obtain the link URL.

 $url = $this->ask('Link URL:');

Then, you’ll use the filter_var function to validate that the input obtained from the user is a valid URL. If the link is invalid, you’ll show an error and exit the application with status code 1, which means the application exited in error.

        if (!filter_var($url, FILTER_VALIDATE_URL)) {
            $this->error("Invalid URL. Exiting...");
            return 1;
        }

If the link is valid, you’ll continue and ask for the link description using the same method as before.

 $description = $this->ask('Link Description:');

You’ll then ask for a final confirmation that all data is correct, using the confirm helper. If the user confirms, the link is finally inserted in the database. You’ll use the Link Eloquent model created in a previous part of this series to interact with the database.

        $this->info("New Link:");
        $this->info($url . ' - ' . $description);

        if ($this->confirm('Is this information correct?')) {
            $link = new Link();
            $link->url = $url;
            $link->description = $description;
            $link->save();

            $this->info("Saved.");
        }

The application exits with a 0, representing a success status (0 errors).

return 0;

The following code contains the full implementation of these steps. Replace the current content in your LinkNew class with:

app/Console/Commands/LinkNew.php
<?php

namespace App\Console\Commands;

use App\Models\Link;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class LinkNew extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'link:new';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a New Link';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $url = $this->ask('Link URL:');

        if (!filter_var($url, FILTER_VALIDATE_URL)) {
            $this->error("Invalid URL. Exiting...");
            return 1;
        }

        $description = $this->ask('Link Description:');

        $this->info("New Link:");
        $this->info($url . ' - ' . $description);

        if ($this->confirm('Is this information correct?')) {
            $link = new Link();
            $link->url = $url;
            $link->description = $description;
            $link->save();

            $this->info("Saved.");
        }

        return 0;
    }
}

Save and close the file when you’re done.

To execute the command and insert a new link in the database, run:

docker-compose exec app php artisan link:new
Output
Link URL:: > https://digitalocean.com/community Link Description:: > DigitalOcean Community New Link: https://digitalocean.com/community - DigitalOcean Community Is this information correct? (yes/no) [no]: > yes Saved.

Feel free to add a few more links if you want to.

Listing Links

Next, you’ll need to create a new Artisan command to show the list of all links.You can call it link:list. Create the new command with:

docker-compose exec app php artisan make:command LinkList

Open the command class using your text or code editor of choice:

nano app/Console/Commands/LinkList.php

Within the handle method of this command, you’ll query for all rows in the links table. You can use the Link model to access the underlying database query methods that Eloquent provides. To exhibit the results nicely in the command line, you can use the table output helper:

        $headers = [ 'id', 'url', 'description' ];
        $links = Link::all(['id', 'url', 'description'])->toArray();
        $this->table($headers, $links);

        return 0;

The following code contains the full implementation of the link:list command. Replace the content in your LinkList.php file with :

app/Console/Commands/LinkList.php
<?php

namespace App\Console\Commands;

use App\Models\Link;
use Illuminate\Console\Command;

class LinkList extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'link:list';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'List links saved in the database';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $headers = [ 'id', 'url', 'description' ];
        $links = Link::all(['id', 'url', 'description'])->toArray();
        $this->table($headers, $links);

        return 0;
    }
}

Save and close the file when you are done.

To run this command and show a list of all links already inserted in the linkstable, run:

docker-compose exec app php artisan link:list
Output
+----+------------------------------------+--------------+ | id | url | description | +----+------------------------------------+--------------+ | 1 | https://digitalocean.com/community | DO Community | | 2 | https://laravel.com | Laravel | +----+------------------------------------+--------------+

Deleting Links

Finally, you’ll create a command to delete links:

docker-compose exec app php artisan make:command LinkDelete
Output
Console command created successfully.

Open the new file using your text or code editor of choice:

nano app/Console/Commands/LinkDelete.php

You can name this command link:delete. To know which link must be deleted, you’ll need to require that users provide an additional argument when calling the command: the ID of the link. This is also set within the $signature variable, which defines how your command is called and what arguments, mandatory or not, should be provided:

protected $signature = 'link:delete {link_id}';

The handle method for this command will implement a few different instructions. First, you’ll obtain the Link ID that should have been provided within the command call.

$link_id = $this->argument('link_id');

Then, you’ll obtain the referenced link from the database, using the Eloquent method find that is available through your Link model.

$link = Link::find($link_id);

When the find method doesn’t find a database record with that ID, it will return null. You’ll check if that is the current value contained in the $link variable, and return an error in that case. The program will exit in error (code 1).

        if ($link === null) {
            $this->error("Invalid or non-existent link ID.");
            return 1;
        }

When $link is not null, the command continues execution. You then use the confirm helper to ask for a user confirmation.

if ($this->confirm('Are you sure you want to delete this link? ' . $link->url)) {
    // deletes link
}

When the user confirms the action by typing yes and hitting ENTER, you’ll call the delete method from the Link Eloquent model to delete the specified link from the database.

            $link->delete();
            $this->info("Link deleted.");

The following code contains the full implementation for the list:delete command. Replace the content in your LinkDelete.php file with the following:

app/Console/Commands/LinkDelete.php
<?php

namespace App\Console\Commands;

use App\Models\Link;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class LinkDelete extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'link:delete {link_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Deletes a link from the database.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $link_id = $this->argument('link_id');
        $link = Link::find($link_id);

        if ($link === null) {
            $this->error("Invalid or non-existent link ID.");
            return 1;
        }

        if ($this->confirm('Are you sure you want to delete this link? ' . $link->url)) {
            $link->delete();
            $this->info("Link deleted.");
        }

        return 0;
    }
}

Save and close the file when you’re done.

Now when you want to delete a link from your links table, you’ll first need to obtain the link’s ID with artisan link:list, as demonstrated earlier on. Once you know the ID of a link, you can run the artisan link:delete command with:

docker-compose exec app php artisan link:delete LINK_ID
Output
Are you sure you want to delete this link? https://laravel.com (yes/no) [no]: > yes Link deleted.

You’re now able to insert, list, and delete links in the application’s database, using Artisan commands executed from a command-line interface. In the next part of this series, you’ll set up the front end of your application using Blade templates and the Bulma CSS framework.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK