Lesson 5 of 50 – Laravel Artisan Commands
10%

Laravel Artisan Commands

Artisan is Laravel's command-line interface (CLI). It helps developers create application files, inspect routes, manage database migrations, clear caches, and perform many other development tasks.

In this lesson, you will learn commonly used Artisan commands with practical examples.

1. What Is Laravel Artisan?

Artisan is included with a Laravel application. Its command-line entry point is the artisan file in your project root.

To run Artisan commands, open a terminal in your Laravel project directory.

cd student-app

php artisan

The command displays the available commands for your installed Laravel application.

Examples of tasks performed with Artisan:

  • Create controllers and models.
  • Generate database migrations.
  • Display registered routes.
  • Run database migrations.
  • Start the local development server.
  • Clear application caches.
2. Display Laravel Version and Available Commands

Display the installed Laravel version:

php artisan --version

This command displays the Laravel framework version used by your application.

List available Artisan commands:

php artisan list

You can also inspect the help for a particular command.

php artisan help make:controller

The help output describes the command's usage, arguments, and options.

Check the current application's routes:

php artisan route:list

The route list helps you inspect registered routes, their HTTP methods, names, and associated actions.

3. Create Controllers Using Artisan

Controllers organize request-handling logic. Artisan can generate a controller file for you.

Create a basic controller:

php artisan make:controller StudentController

Laravel creates a controller in:

app/Http/Controllers/StudentController.php

Create a resource controller:

php artisan make:controller StudentController --resource

A resource controller includes methods commonly used for CRUD operations, such as:

index()
create()
store()
show()
edit()
update()
destroy()

These methods provide a starting structure. You must implement the required application logic yourself.

Example controller method:

<?php

namespace App\Http\Controllers;

class StudentController extends Controller
{
    public function index()
    {
        return 'Welcome to Student Management';
    }
}
4. Create Models Using Artisan

Eloquent models represent and work with application data. You can generate a model using Artisan.

php artisan make:model Student

Laravel creates the model at:

app/Models/Student.php

Create a model with a migration:

php artisan make:model Student -m

The -m option also generates a migration file for the model's database table.

Create a model with related files:

php artisan make:model Student -mcr

This combines options to create a migration, controller, and resource controller methods with the model. The precise generated files depend on the installed Laravel version and command options.

For beginners, creating the model and migration separately can make each step easier to understand.

5. Create and Run Database Migrations

Migrations let you manage database schema changes through version-controlled PHP files.

Create a students table migration:

php artisan make:migration create_students_table

Open the generated file in database/migrations/ and define the columns inside its up() method.

public function up(): void
{
    Schema::create('students', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('course');
        $table->timestamps();
    });
}

Make sure the migration file includes the required Schema and Blueprint imports generated by Laravel.

Run pending migrations:

php artisan migrate

Check migration status:

php artisan migrate:status

Before running migrations, configure a working database connection in your application's environment settings. Review migrations before running them against production.

6. Start the Laravel Development Server

Artisan can start a local development server so you can test your Laravel application in a browser.

php artisan serve

The terminal displays the local address, commonly:

http://127.0.0.1:8000

Open the address displayed in your terminal.

Choose a different port:

php artisan serve --port=8080

If the port is available, you can then access the application at:

http://127.0.0.1:8080

Use this development server locally. For production, use a properly configured production web server and PHP runtime.

7. Clear Laravel Caches

Laravel provides commands for clearing cached configuration, routes, views, and application data.

Clear the application cache:

php artisan cache:clear

Clear the configuration cache:

php artisan config:clear

Clear the route cache:

php artisan route:clear

Clear compiled Blade views:

php artisan view:clear

Clear common optimization caches:

php artisan optimize:clear

Use these commands when troubleshooting stale cached settings or routes. On production servers, understand the deployment process before clearing or rebuilding caches.

8. Other Useful Artisan Commands

Here are additional commands you may use regularly:

Command Purpose
php artisan make:model Course Create a model.
php artisan make:middleware CheckRole Create middleware.
php artisan make:seeder StudentSeeder Create a database seeder.
php artisan make:factory StudentFactory Create a model factory.
php artisan make:request StoreStudentRequest Create a form request class.
php artisan make:job SendStudentEmail Create a job class.
php artisan make:command ReportStudents Create a custom Artisan command.
php artisan test Run the application's automated tests.

Some commands or generated structures can vary by Laravel version. Use php artisan list to check the commands available in your installation.

9. Practical Assignment

Complete these exercises inside your Laravel project.

  1. Display your Laravel version.
  2. List all available Artisan commands.
  3. Generate a controller named StudentController.
  4. Generate a model named Student.
  5. Create a migration named create_students_table.
  6. Inspect your registered routes.
  7. Start the local development server.
  8. Clear the configuration cache.
  9. Explain the purpose of five Artisan commands.

Practice commands:

php artisan --version

php artisan list

php artisan make:controller StudentController

php artisan make:model Student

php artisan make:migration create_students_table

php artisan route:list

php artisan serve

In the next lesson, you will learn how to define Laravel routes and connect URLs to application logic.


🧠 Quick Quiz

1. Which command displays registered Laravel routes?




2. Which command creates a Laravel controller?




3. Which command runs pending database migrations?