Laravel 常用指令

更新 2026.09.21

Laravel 開發、資料庫、快取、路由、排程與套件管理常用指令整理。

Cache / Config / Route

指令 用途 備註
php artisan cache:clear 清除 Application Cache 清除 Laravel Cache
php artisan config:clear 清除 Config Cache 修改 .env 或設定檔後常用
php artisan config:cache 建立 Config Cache Production 常用
php artisan route:clear 清除 Route Cache 修改 Route 後可使用
php artisan route:cache 建立 Route Cache Production 常用
php artisan view:clear 清除 Blade View Cache Blade 顯示異常時可使用
php artisan view:cache 預先編譯 Blade View Production 可使用
php artisan event:clear 清除 Event Cache
php artisan event:cache 建立 Event Cache
php artisan optimize:clear 清除 Laravel 主要快取 開發環境最常用
php artisan optimize 建立 Laravel 最佳化快取 Production 常用

Model

指令 用途 備註
php artisan make:model User 建立 Model
php artisan make:model User -m 建立 Model + Migration -m = migration
php artisan make:model User -s 建立 Model + Seeder -s = seeder
php artisan make:model User -f 建立 Model + Factory -f = factory
php artisan make:model User -c 建立 Model + Controller -c = controller
php artisan make:model User -mfs 建立 Model + Migration + Factory + Seeder 可組合參數
php artisan make:model User -a 建立 Model 與常用相關檔案 -a = all

Controller

指令 用途 備註
php artisan make:controller UserController 建立 Controller
php artisan make:controller UserController --resource 建立 Resource Controller 自動產生 CRUD 基本方法
php artisan make:controller UserController --api 建立 API Controller 不包含 create、edit
php artisan make:controller UserController --resource --model=User 建立對應 Model 的 Resource Controller

Resource Controller 預設包含:

index
create
store
show
edit
update
destroy

Migration

指令 用途 備註
php artisan make:migration create_users_table 建立 Migration
php artisan make:migration add_image_token_to_user_jwt_tokens_table --table=user_jwt_tokens 修改既有 Table 的 Migration
php artisan migrate 執行尚未執行的 Migration
php artisan migrate:status 查看 Migration 狀態
php artisan migrate:rollback 回復上一批 Migration
php artisan migrate:rollback --step=1 回復指定數量 Migration
php artisan migrate:reset 回復所有 Migration
php artisan migrate:refresh Rollback 全部後重新 Migration
php artisan migrate:refresh --seed 重建 Migration 並執行 Seeder 開發環境常用
php artisan migrate:fresh Drop 所有 Table 並重新 Migration 資料會全部刪除
php artisan migrate:fresh --seed Drop 所有 Table、重新 Migration 並 Seed 本機開發常用

migrate:fresh 會直接刪除資料表,不適合直接在正式環境使用。

Seeder

指令 用途 備註
php artisan make:seeder UserSeeder 建立 Seeder
php artisan db:seed 執行 DatabaseSeeder
php artisan db:seed --class=UserSeeder 執行指定 Seeder
php artisan migrate:fresh --seed 重建 Database 並執行 Seeder

Factory

指令 用途 備註
php artisan make:factory UserFactory 建立 Factory
php artisan make:factory UserFactory --model=User 建立指定 Model 的 Factory

Route

指令 用途 備註
php artisan route:list 查看所有 Route
php artisan route:list --path=api 查看指定 Path Route
php artisan route:list --name=user 搜尋指定 Route Name
php artisan route:clear 清除 Route Cache
php artisan route:cache 建立 Route Cache

Schedule

指令 用途 備註
php artisan schedule:list 查看所有 Scheduled Tasks
php artisan schedule:run 執行目前應執行的排程 Cron 常用
php artisan schedule:work 持續執行 Scheduler 本機開發方便使用

Linux Cron:

* * * * * cd /path/to/project && php artisan schedule:run >> /dev/null 2>&1

Queue

指令 用途 備註
php artisan queue:work 啟動 Queue Worker
php artisan queue:work --tries=3 Job 最多嘗試 3 次
php artisan queue:restart 重新啟動 Queue Worker Deploy 後常用
php artisan queue:failed 查看失敗 Job
php artisan queue:retry all 重試所有失敗 Job
php artisan queue:flush 清除失敗 Job 記錄

Artisan Generate

指令 用途 備註
php artisan make:command ExampleCommand 建立 Artisan Command
php artisan make:job ExampleJob 建立 Job
php artisan make:event ExampleEvent 建立 Event
php artisan make:listener ExampleListener 建立 Listener
php artisan make:mail ExampleMail 建立 Mail
php artisan make:notification ExampleNotification 建立 Notification
php artisan make:middleware ExampleMiddleware 建立 Middleware
php artisan make:request UserRequest 建立 Form Request Validation 常用
php artisan make:resource UserResource 建立 API Resource
php artisan make:policy UserPolicy 建立 Policy
php artisan make:provider ExampleServiceProvider 建立 Service Provider

Storage

指令 用途 備註
php artisan storage:link 建立 Public Storage Symbolic Link public/storage → storage/app/public

Tinker

指令 用途 備註
php artisan tinker 開啟 Laravel REPL 可直接操作 Model / Service

例如:

User::count();

User::find(1);

User::factory()->count(10)->create();

Test

指令 用途 備註
php artisan test 執行 Laravel Tests
php artisan test --filter=UserTest 執行指定 Test
php artisan make:test UserTest 建立 Feature Test
php artisan make:test UserTest --unit 建立 Unit Test

Application

指令 用途 備註
php artisan serve 啟動 Laravel Development Server 預設 127.0.0.1:8000
php artisan about 查看 Laravel Application 資訊
php artisan list 查看所有 Artisan Command
php artisan help migrate 查看指定 Command 說明

Composer

指令 用途 備註
composer install 安裝 composer.lock 指定的 Dependencies Clone Project 後常用
composer update 更新 Dependencies 會更新 composer.lock
composer dump-autoload 重新建立 Composer Autoload 新增 Class 找不到時可使用
composer dump-autoload -o 建立最佳化 Autoload Production 常用

NPM / Frontend

指令 用途 備註
npm install 安裝 Frontend Dependencies
npm run dev 啟動 Development Build Vite
npm run build 建立 Production Build Vite

Laravel AdminLTE

套件:

jeroennoten/Laravel-AdminLTE
指令 用途
php artisan adminlte:plugins 查看 AdminLTE Plugins
php artisan adminlte:plugins install --plugin=PLUGIN_NAME 安裝指定 AdminLTE Plugin

例如:

php artisan adminlte:plugins
php artisan adminlte:plugins install --plugin=datatables

實際可用指令依 jeroennoten/laravel-adminlte 套件版本而異。

Laravel Echo Server

舊版 laravel-echo-server 專案可能使用:

./node_modules/laravel-echo-server/bin/server.js start

Docker 環境亦可能是:

/workdir/application/node_modules/laravel-echo-server/bin/server.js start

或:

npx laravel-echo-server start

laravel-echo-server 屬於較舊的 Socket.IO Laravel Broadcasting 架構,新專案需依實際使用的 Broadcasting Server 設定。

常用開發流程

第一次 Clone Project:

composer install
npm install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan storage:link
npm run dev
php artisan serve

Database 全部重建:

php artisan migrate:fresh --seed

修改 .env / Config 後:

php artisan config:clear

開發時發生 Cache 問題:

php artisan optimize:clear

Production Deploy 常見:

composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize
php artisan queue:restart
npm run build

自訂 Artisan Command

建立:

php artisan make:command ExampleCommand

執行:

php artisan example:command