---
title: "Artisan Commands"
canonical_url: "https://handbook.propay.co.za/laravel/artisan-commands"
---
# Artisan Commands

## Names

The names given to artisan commands SHOULD all be kebab-cased.

<code-group>

```php [PHP]
// GOOD
php artisan delete-old-records

// BAD
php artisan deleteOldRecords

// BAD
php artisan delete_old_records
```

</code-group>

## Constructors

Inject any dependencies in the `handle()` method instead of in the constructor. Laravel initiates ALL console commands on every `artisan` call, for this reason console command class constructors should be fast and not contain any heavy logic.

## Output

Use verbosity levels, and use different levels for different environments and for debugging.

<warning>

This is important because you want a lot of information available when you need to debug the script, but you do not want to log every output on every run and fill up the production log files.

</warning>

<code-group>

```php [PHP]
$this->info('Updating Articles...', 'v');
// ...
foreach($articles as $article) {
    // ...
    $this->info("\t Article #{$article->id} has been updated", 'vvv');
}

$this->info("{$articles->count()} Articles has been updated");
```

</code-group>

Different output levels

- quiet mode: only errors and important warnings.
- normal mode: errors, all warnings and general feedback like `All OK, processed XX records!`.
- v, vv, vvv modes: errors, warnings and any additional info.

The idea behind it is to send email with console command outputs only when output is present (not empty).

Use non-zero exit codes on errors

Use non-zero exit codes if a command execution failed (alternatively throw an exception — this is the same as exit code 1). This allows you to use global on-error handlers, e.g. for automated reporting about failed console commands, please see `\Illuminate\Console\Scheduling\Event::emailOutputOnFailure` as an example.


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
