DEV Community

Sadiul Hakim
Sadiul Hakim

Posted on

Laravel Artisan Commands – Practical Tutorial

php artisan is Laravel’s command-line interface. It helps you generate code, manage the app lifecycle, work with the database, debug, and maintain the project efficiently.


1. Controller Commands

1.1 Basic Controller

php artisan make:controller UserController
Enter fullscreen mode Exit fullscreen mode

What it does

  • Creates an empty controller class in app/Http/Controllers

Use case

  • When you want full control and plan to manually define methods.

Example

class UserController extends Controller {
    public function index() {}
    public function store() {}
}
Enter fullscreen mode Exit fullscreen mode

1.2 Resource Controller

php artisan make:controller UserController --resource
Enter fullscreen mode Exit fullscreen mode

What it generates

  • A controller with CRUD methods

Methods included:

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

Use case

  • Best for MVC web apps
  • Works perfectly with:
Route::resource('users', UserController::class);
Enter fullscreen mode Exit fullscreen mode

1.3 API Resource Controller

php artisan make:controller UserController --api
Enter fullscreen mode Exit fullscreen mode

Difference from --resource

  • Removes UI-related methods:

    • create()
    • edit()

Use case

  • REST APIs (JSON only)
  • Common in Sanctum / JWT based APIs

1.4 Invokable Controller

php artisan make:controller UserController --invokable
Enter fullscreen mode Exit fullscreen mode

What it creates

  • A controller with only __invoke()

Use case

  • Single-responsibility routes
  • Clean and readable for small actions

Example

Route::post('/login', LoginController::class);
Enter fullscreen mode Exit fullscreen mode

2. View & UI Related Commands

2.1 Create a Blade View

php artisan make:view about
Enter fullscreen mode Exit fullscreen mode

Creates

resources/views/about.blade.php
Enter fullscreen mode Exit fullscreen mode

Use case

  • Quickly scaffold views for pages

2.2 Blade Component

php artisan make:component About
Enter fullscreen mode Exit fullscreen mode

Creates

  • Component class
  • Blade view

Use case

  • Reusable UI elements
  • Headers, buttons, modals

Example

<x-about />
Enter fullscreen mode Exit fullscreen mode

3. Model & Database Layer

3.1 Create Model

php artisan make:model Course
Enter fullscreen mode Exit fullscreen mode

Creates

app/Models/Course.php
Enter fullscreen mode Exit fullscreen mode

Common variants

php artisan make:model Course -m   # with migration
php artisan make:model Course -f   # with factory
php artisan make:model Course -s   # with seeder
php artisan make:model Course -a   # all
Enter fullscreen mode Exit fullscreen mode

3.2 Show Model Info (Laravel 10+)

php artisan model:show Course
Enter fullscreen mode Exit fullscreen mode

Displays

  • Table name
  • Columns
  • Relationships
  • Casts

Use case

  • Debugging models
  • Understanding schema quickly

4. Migration Commands

4.1 Create Migration

php artisan make:migration create_posts_table
Enter fullscreen mode Exit fullscreen mode

Creates

database/migrations/xxxx_create_posts_table.php
Enter fullscreen mode Exit fullscreen mode

4.2 Run Migrations

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Use case

  • Apply schema changes

4.3 Rollback Migrations

php artisan migrate:rollback --step=3
Enter fullscreen mode Exit fullscreen mode

Use case

  • Undo last 3 migrations

4.4 Reset All Migrations

php artisan migrate:reset
Enter fullscreen mode Exit fullscreen mode

Danger

  • Drops all tables

4.5 Refresh Migrations

php artisan migrate:refresh
Enter fullscreen mode Exit fullscreen mode

Does

  • Rollback + re-run migrations

Useful during development.


4.6 Run a Specific Migration

php artisan migrate --path=/database/migrations/2026_01_04_150744_create_teachers_table.php
Enter fullscreen mode Exit fullscreen mode

Use case

  • Fixing or testing one migration

5. Seeder & Database Data

5.1 Create Seeder

php artisan make:seeder TeachersSeeder
Enter fullscreen mode Exit fullscreen mode

5.2 Run Seeder

php artisan db:seed --class=TeachersSeeder
Enter fullscreen mode Exit fullscreen mode

Use case

  • Insert test or default data

6. Validation, Middleware, Security

6.1 Custom Validation Rule

php artisan make:rule Uppercase
Enter fullscreen mode Exit fullscreen mode

Use case

  • Reusable complex validation logic

6.2 Middleware

php artisan make:middleware CheckRole
Enter fullscreen mode Exit fullscreen mode

Use case

  • Authentication
  • Authorization
  • Request filtering

7. Authentication & API

7.1 Install API Authentication

php artisan install:api
Enter fullscreen mode Exit fullscreen mode

Installs

  • Laravel Sanctum
  • API auth scaffolding

7.2 Auth Service Provider

php artisan make:provider AuthServiceProvider
Enter fullscreen mode Exit fullscreen mode

Use case

  • Define Gates & Policies
  • Authorization logic

8. Mail & Communication

8.1 Create Mail Class

php artisan make:mail WelcomeMail
Enter fullscreen mode Exit fullscreen mode

Use case

  • Emails
  • Notifications

9. Application Setup & Maintenance

9.1 Generate App Key

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

Must run

  • After cloning a project
  • Required for encryption, sessions, cookies

9.2 Storage Symlink

php artisan storage:link
Enter fullscreen mode Exit fullscreen mode

Purpose

  • Makes storage/app/public accessible from browser

Use case

  • File uploads
  • Images

9.3 Maintenance Mode

php artisan down
php artisan down --secret="token"
php artisan up
Enter fullscreen mode Exit fullscreen mode

Use case

  • Deployment
  • Server maintenance

10. Routing & Debugging

10.1 List Routes

php artisan route:list
Enter fullscreen mode Exit fullscreen mode
php artisan route:list --except-vendor
Enter fullscreen mode Exit fullscreen mode

Use case

  • Debug route conflicts
  • Check middleware, methods

11. Localization & Stubs

11.1 Enable Localization

php artisan lang:publish
Enter fullscreen mode Exit fullscreen mode

Creates

lang/en/
Enter fullscreen mode Exit fullscreen mode

Use case

  • Multi-language apps

11.2 Publish Stubs

php artisan stub:publish
Enter fullscreen mode Exit fullscreen mode

What is a stub?

  • Template used by make: commands

Use case

  • Customize default controllers, models, migrations

12. Cache & Optimization

php artisan optimize
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:clear
Enter fullscreen mode Exit fullscreen mode

Use case

  • Production performance
  • Fix weird cache bugs

13. Queue & Jobs

php artisan make:job SendEmailJob
php artisan queue:work
Enter fullscreen mode Exit fullscreen mode

14. Policy

php artisan make:policy CoursePolicy --model=Course
Enter fullscreen mode Exit fullscreen mode

15. Tinker (Very Important)

php artisan tinker
Enter fullscreen mode Exit fullscreen mode

Use case

  • Run Laravel code interactively
  • Debug models, queries, services

Top comments (0)