🧭 Laravel Artisan 命令大全
Laravel 的命令行工具叫 Artisan,它用于项目管理、代码生成、数据库迁移、缓存管理、调试等。
执行命令的方式:
bash
php artisan <命令> [选项]🏁 一、基础命令
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan --version | 查看 Laravel 版本 | php artisan --version |
php artisan list | 查看所有可用命令 | php artisan list |
php artisan help <命令> | 查看命令详细说明 | php artisan help migrate |
php artisan inspire | 输出一条励志名言 | php artisan inspire |
php artisan tinker | 进入 REPL 交互模式,可直接操作模型、数据库等 | php artisan tinker |
⚙️ 二、项目管理
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan serve | 启动内置开发服务器 (默认 http://127.0.0.1:8000) | php artisan serve --port=8080 |
php artisan down | 进入维护模式 | php artisan down |
php artisan up | 退出维护模式 | php artisan up |
php artisan key:generate | 生成新的应用密钥 (APP_KEY) | php artisan key:generate |
php artisan config:clear | 清除配置缓存 | php artisan config:clear |
php artisan config:cache | 生成配置缓存文件 | php artisan config:cache |
php artisan route:clear | 清除路由缓存 | php artisan route:clear |
php artisan route:cache | 缓存路由以提高性能 | php artisan route:cache |
php artisan view:clear | 清除视图缓存 | php artisan view:clear |
php artisan cache:clear | 清除应用缓存 | php artisan cache:clear |
🧩 三、代码生成(make)
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan make:controller | 创建控制器 | php artisan make:controller UserController |
php artisan make:model | 创建模型 | php artisan make:model User |
php artisan make:migration | 创建迁移文件 | php artisan make:migration create_users_table |
php artisan make:seeder | 创建数据库填充器 | php artisan make:seeder UserSeeder |
php artisan make:factory | 创建模型工厂 | php artisan make:factory UserFactory |
php artisan make:middleware | 创建中间件 | php artisan make:middleware CheckLogin |
php artisan make:request | 创建表单请求验证类 | php artisan make:request StoreUserRequest |
php artisan make:command | 创建自定义命令 | php artisan make:command SendReportEmail |
php artisan make:job | 创建异步任务 | php artisan make:job ProcessOrderJob |
php artisan make:event | 创建事件类 | php artisan make:event UserRegistered |
php artisan make:listener | 创建事件监听器 | php artisan make:listener SendWelcomeEmail |
php artisan make:mail | 创建邮件类 | php artisan make:mail OrderShipped |
php artisan make:policy | 创建授权策略 | php artisan make:policy PostPolicy |
php artisan make:resource | 创建 API 资源类 | php artisan make:resource UserResource |
php artisan make:test | 创建测试类 | php artisan make:test UserTest |
🗄️ 四、数据库相关
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan migrate | 执行迁移 | php artisan migrate |
php artisan migrate:rollback | 回滚最近一次迁移 | php artisan migrate:rollback |
php artisan migrate:reset | 回滚所有迁移 | php artisan migrate:reset |
php artisan migrate:refresh | 重置并重新执行所有迁移 | php artisan migrate:refresh --seed |
php artisan db:seed | 运行数据填充器 | php artisan db:seed --class=UserSeeder |
php artisan migrate:status | 查看迁移执行状态 | php artisan migrate:status |
php artisan migrate:fresh | 删除所有表并重新迁移 | php artisan migrate:fresh --seed |
🧮 五、缓存与优化
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan optimize | 优化框架性能 | php artisan optimize |
php artisan config:cache | 缓存配置文件 | php artisan config:cache |
php artisan route:cache | 缓存路由 | php artisan route:cache |
php artisan event:cache | 缓存事件与监听器 | php artisan event:cache |
php artisan view:cache | 预编译 Blade 视图 | php artisan view:cache |
php artisan cache:clear | 清除缓存 | php artisan cache:clear |
🔔 六、队列与任务调度
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan queue:work | 处理队列中的任务 | php artisan queue:work --tries=3 |
php artisan queue:listen | 监听队列并处理任务 | php artisan queue:listen |
php artisan queue:retry | 重试失败任务 | php artisan queue:retry 5 |
php artisan queue:failed | 查看失败的任务列表 | php artisan queue:failed |
php artisan queue:flush | 清空所有失败任务 | php artisan queue:flush |
php artisan schedule:run | 手动运行调度任务 | php artisan schedule:run |
🧪 七、测试与调试
| 命令 | 说明 | 示例 |
|---|---|---|
php artisan test | 运行测试 | php artisan test --filter=UserTest |
php artisan route:list | 查看所有路由 | php artisan route:list |
php artisan env | 查看当前环境 | php artisan env |
php artisan about | 查看项目配置、版本等信息 | php artisan about |
🧰 八、自定义命令示例
1️⃣ 创建命令
bash
php artisan make:command SendReportEmail2️⃣ 编辑命令文件(app/Console/Commands/SendReportEmail.php)
php
public function handle()
{
\Log::info('Report email sent successfully.');
$this->info('Report email has been sent!');
}3️⃣ 执行命令
bash
php artisan send:report-email🧾 九、实用组合命令示例
| 场景 | 命令 |
|---|---|
| 初始化新项目 | composer create-project laravel/laravel blog |
| 启动本地服务器 | php artisan serve |
| 数据库迁移 + 填充 | php artisan migrate --seed |
| 优化部署 | php artisan optimize && php artisan config:cache && php artisan route:cache |
