0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🚀 Laravelのよく使うArtisanコマンド一覧と効率化Tips

Posted at

Laravel開発では php artisan コマンドを頻繁に使用しますが、
毎回入力するのはちょっと面倒ですよね…(;´Д`)

この記事では、よく使うArtisanコマンドをカテゴリ別に整理し、
さらに入力の手間を省く カスタムコマンド run:menu の作成方法も紹介します!
初心者から上級者まで、ぜひ開発効率アップに役立ててください 🎉


📑 1. Laravel Artisanコマンド一覧表

🔧 開発サーバー関連

コマンド 説明 使用例
php artisan serve ローカル開発サーバーを起動(デフォルト: 8000番ポート) php artisan serve --port=8080

🗄️ データベース関連

コマンド 説明 使用例
php artisan migrate DBマイグレーションを実行 php artisan migrate
php artisan migrate:fresh DBリセット後に再マイグレーション php artisan migrate:fresh --seed
php artisan db:seed シーダーを実行 php artisan db:seed --class=UsersTableSeeder
php artisan migrate:rollback 直前のマイグレーションを取り消し php artisan migrate:rollback

🧪 テスト関連

コマンド 説明 使用例
php artisan test PHPUnitテストを実行 php artisan test --filter TestClass
php artisan dusk Laravel Duskでブラウザテスト php artisan dusk

🏗️ コード生成関連

コマンド 説明 使用例
php artisan make:model モデル作成 php artisan make:model Post -m
php artisan make:controller コントローラ作成 php artisan make:controller PostController --resource
php artisan make:migration マイグレーション作成 php artisan make:migration create_posts_table
php artisan make:seeder シーダー作成 php artisan make:seeder UsersTableSeeder
php artisan make:factory ファクトリ作成 php artisan make:factory PostFactory

⚡ キャッシュ管理関連

コマンド 説明 使用例
php artisan cache:clear アプリキャッシュを削除 php artisan cache:clear
php artisan config:cache 設定ファイルをキャッシュ php artisan config:cache
php artisan route:cache ルートをキャッシュ php artisan route:cache
php artisan view:cache Bladeをキャッシュ php artisan view:cache

🐛 デバッグ・情報表示関連

コマンド 説明 使用例
php artisan tinker 対話型REPLを起動 php artisan tinker
php artisan route:list ルート一覧を表示 php artisan route:list --except-vendor
php artisan optimize アプリを最適化 php artisan optimize

🎮 2. カスタムコマンドで効率化! php artisan run:menu

毎回コマンドを手打ちするのは大変…
そんなときは 番号選択式のカスタムコマンド を作っちゃいましょう😎✨

コマンド作成

php artisan make:command RunMenu

コード編集(app/Console/Commands/RunMenu.php)

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

class RunMenu extends Command
{
    protected $signature = 'run:menu';
    protected $description = 'よく使うArtisanコマンドを一覧表示し、選択して実行';

    protected $commands = [
        1 => ['name' => '開発サーバー起動', 'command' => 'serve'],
        2 => ['name' => 'マイグレーション実行', 'command' => 'migrate'],
        3 => ['name' => 'テスト実行', 'command' => 'test'],
        4 => ['name' => 'Tinker起動', 'command' => 'tinker'],
        5 => ['name' => 'マイグレーション初期化', 'command' => 'migrate:fresh'],
        6 => ['name' => 'モデル作成', 'command' => 'make:model'],
        7 => ['name' => 'キャッシュクリア', 'command' => 'cache:clear'],
        8 => ['name' => 'ルート一覧表示', 'command' => 'route:list'],
    ];

    public function handle()
    {
        while (true) {
            $this->info('=== Laravel Artisan コマンド選択 ===');
            foreach ($this->commands as $index => $cmd) {
                $this->line("$index. {$cmd['name']} (php artisan {$cmd['command']})");
            }
            $this->line('0. 終了');

            $choice = $this->ask('選択してください (0-' . count($this->commands) . ')');

            if ($choice === '0') {
                $this->info('スクリプトを終了します。');
                break;
            }

            if (isset($this->commands[$choice])) {
                $this->info("実行中: php artisan {$this->commands[$choice]['command']}");
                try {
                    $this->call($this->commands[$choice]['command']);
                } catch (\Exception $e) {
                    $this->error('エラー: ' . $e->getMessage());
                }
            } else {
                $this->error('無効な選択です。');
            }
        }
    }
}

💻 3. XAMPP環境での実行

Windows (例: C:\xampp\htdocs) での注意点👇

  1. PHPパス確認

    C:\xampp\php\php.exe -v
    

    もし動かなければ、環境変数 PATH に C:\xampp\php を追加。

  2. Laravelプロジェクトへ移動

    cd C:\xampp\htdocs\my-laravel-app
    
  3. コマンド実行

    C:\xampp\php\php.exe artisan run:menu
    
  4. 文字化け対策

    chcp 65001
    

⚠️ 4. 注意点

  • ✅ エラーハンドリング済み(例外はキャッチしてメッセージ表示)
  • 🔒 ユーザー入力は番号のみで安全
  • 🔧 プロジェクトごとに $commands 配列をカスタマイズ可能
  • 📝 パラメータ付きコマンド(例: make:model)は追加の入力処理を実装すると便利

🎯 5. まとめ

  • よく使うArtisanコマンドを カテゴリ別一覧表 に整理 📚
  • 番号選択式の カスタムコマンド run:menu を実装 🎮
  • XAMPP環境でも簡単に実行可能 💻

これでLaravel開発がサクサク進みますね!✨
ぜひプロジェクトに応じてカスタマイズしてみてください (๑•̀ㅂ•́)و✧

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?