Laravel for Beginners: Using Custom and Built-in Commands

Arlind Musliu cofounder at Lucky Media
Arlind Musliu

February 7, 2025 · 5 min read

Laravel for Beginners Using Custom and Built-in Commands

2026 UPDATE - LARAVEL 13

We’re excited to announce that we have updated all of our blog post examples to reflect the new Laravel 13 version! Our previous examples were based on Laravel 10, but with the release of Laravel 13, we wanted to ensure that our readers have access to the most up-to-date information and examples.

What is Artisan in Laravel?

Artisan is the command-line interface included with Laravel, providing a variety of helpful commands to assist in building your application.

With Artisan, you can:

  • Generate boilerplate code

  • Run database migrations

  • Start a development server

  • And much more!

Laravel also allows developers to create custom Artisan commands to extend its functionality.

Why use Artisan commands?

Artisan commands save time by automating repetitive tasks and making complex operations simpler. They help you:

  • Work more efficiently

  • Reduce errors

  • Keep things consistent across different environments

laravel best framework php

Built-in Artisan commands

Laravel comes with many built-in Artisan commands that cover a wide range of functionalities. Here are some commonly used categories of built-in commands:

Generating code

Artisan commands can generate different components of a Laravel application, such as controllers, models, migrations, and more. We already used some of these in our previous lessons when we learned about controllers, models, database migrations and seeders:

  • php artisan make:controller PostController: Creates a new controller.

  • php artisan make:model Post: Creates a new model.

  • php artisan make:migration create_posts_table: Creates a new database migration.

  • php artisan make:seeder PostSeeder: Creates a new database seeder.

Database operations

Artisan commands simplify database operations such as running migrations, rolling back migrations, and seeding the database.

  • php artisan migrate: Runs all pending migrations.

  • php artisan migrate:rollback: Rolls back the last database migration.

  • php artisan db:seed: Runs the database seeders.

  • php artisan migrate:fresh --seed: Deletes all data in the database and runs the seeders.

Development tools

Artisan provides commands to facilitate development tasks, such as running a development server, clearing caches, and more.

  • php artisan serve: Starts a local development server.

  • php artisan cache:clear: Clears the application cache.

  • php artisan optimize:clear: Removes all the cache data generated by the optimize command.

  • php artisan route:list: Lists all registered routes.

Application management

Artisan commands assist in managing the application, such as managing queues, scheduling tasks, and more.

  • php artisan queue:work: Starts processing jobs on the queue.

  • php artisan queue:retry all: Retries all the failed jobs in the queue.

  • php artisan queue:flush: Deletes all the failed jobs in the queue.

  • php artisan schedule:run: Runs the scheduled tasks.

Finding the best Laravel 11 tutorial guide for Beginners

Creating custom Artisan commands

In addition to built-in commands, Laravel allows you to create custom Artisan commands to match your specific needs. Follow these steps to create a custom command for deleting unpublished posts older than a specified number of days:

Step 1: Generate the command

First, generate a new custom command using Artisan. This can be done in your terminal by running:

php artisan make:command DeleteOldUnpublishedPosts

This will create a new file in app/Console/Commands/DeleteOldUnpublishedPosts.php.

Step 2: Define the command

Let’s open the newly created command file and modify it. Here’s an example of how you might implement the command:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Post;
use Carbon\Carbon;

class DeleteOldUnpublishedPosts extends Command
{
    // The name and signature of the console command.
    protected $signature = 'posts:cleanup {days=30}';

    // The console command description.
    protected $description = 'Delete unpublished blog posts older than a specified number of days';

    // Execute the console command.
    public function handle()
    {
        $days = $this->argument('days');
        $date = Carbon::now()->subDays($days);

        $deletedCount = Post::where('status', 'unpublished')
                            ->where('created_at', '<', $date)
                            ->delete();

        $this->info("Deleted $deletedCount unpublished posts older than $days days.");
    }
}

Step 3: Register the task scheduler

In this step, we’ll define our commands in the console.php file along with the scheduled time for running each command. We can use functions like ->everyFiveMinutes(), ->daily(), ->hourly(), etc. We need to modify our routes/console.php file. In this example, it’s enough to run the command every day to see if there are unpublished posts older than 30 days and delete them.

use Illuminate\Support\Facades\Schedule;
  
Schedule::command('posts:cleanup')->daily();

Step 4: Run the custom command

Now you can run your custom command using Artisan. By default, it will delete unpublished posts older than 30 days, but you can specify a different number of days if needed. Here’s how to run the command without specifying the number of days:

php artisan posts:cleanup

Or, to specify a different number of days:

php artisan posts:cleanup 60

This command will delete unpublished posts older than 60 days and display the number of deleted posts in the console.

Step 5: Run the scheduler command

Now, we’ll run the custom-created command using the following Laravel artisan command. This will keep our scheduler running and will execute the command every day as we specified.

php artisan schedule:run

Using Laravel Pipelines

Conclusion

Artisan commands are powerful tools that make Laravel development easier. Whether you’re generating code, managing databases, or automating tasks, Artisan commands help you work more efficiently and keep your application consistent.

Upcoming Articles in the Series

  1. Laravel for Beginners: Using Policies for Authorization

  2. Laravel for Beginners: Roles and Permissions with Spatie package

  3. Laravel for Beginners: Differences of using Policies vs Roles and Permissions

This article is part of our series Laravel for Beginners: A Step-by-Step Guide to Learn the Concepts.


Bring Your Ideas to Life 🚀

If you need help with a Laravel project let’s get in touch.

Lucky Media is proud to be recognized as a leading Laravel Development Agency

Technologies

Laravel
Arlind Musliu cofounder at Lucky Media
Arlind Musliu

Cofounder and CFO of Lucky Media

Stay up-to-date

Be updated with all news, products and tips we share!

Let’s chat

We partner with a limited number of brands each quarter to ensure senior-level attention on every project.

lokman and arlind headshots
Teamwork

Related posts

July 31, 2024

Laravel guide for React developers
Laravel
React

July 19, 2024

Comparing Laravel and .NET
Laravel