Laravel開発で役立つ artisanコマンド集&使い方解説
記事の概要
開発現場で頻出のコマンドをまとめました。
メモとしてお使いください。
いいねとフォローお願いします!
マイグレーション
-
「テーブル」や「カラム」などの構造を作ったり変更する際に使用
// テーブル作成 php artisan make:migration create_users_table // ファイル指定してテーブル作成 php artisan migrate --path=/database/migrations/2023_01_01_000000_create_users_table.php // カラム追加・削除 php artisan make:migration add_age_to_users_table --table=users php artisan make:migration remove_age_from_users_table --table=users // migration 実行 php artisan migrate // ステータス確認 php artisan migrate:status // ロールバック php artisan migrate:rollback php artisan migrate:rollback --step=2 php artisan migrate:rollback --path=database/migrations/ファイル名 // テーブルを削除してmigration実行 php artisan migrate:fresh // 既存のテーブルは残してmigrationを全て取り消し・再実行 php artisan migrate:refresh php artisan migrate:refresh --step=1 --path=/database/migrations/ファイル名 // テーブルを作成してシーダーを実行 php artisan migrate:fresh --seed // テーブルとモデルを同時に作成 php artisan make:model User -m
具体的な使い方はこちら
- 詳細ページ作成中です。お待ちください
今後もコマンド追加します。いいねしてお待ちください
シーディング
php artisan make:seeder SeederName
php artisan db:seed
php artisan db:seed --class=UserSeeder
ルーティング
web.phpやapi.phpなどでrouteが正しく定義されているか確認する際に使用
php artisan route:list
php artisan route:list --name=login
php artisan route:list --method=GET
テスト
// 作成
php artisan make:test SampleTest
php artisan make:test SampleTest --unit
// 実行
php artisan test
php artisan test tests/Feature/SampleTest.php // ファイル名指定
php artisan test --filter=クラス名 // クラス名指定
php artisan test --filter=クラス名::メソッド名
ファイル作成
php artisan make:controller HelloController
php artisan make:model User
キャッシュクリア
- 原因不明な現象が起きた時にとりあえず試してみる「おまじない」コマンド
キャッシュが原因でファイルの変更が反映されていないかも。
ビルドする際は忘れずに実行しましょう。
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
その他
php artisan list
php artisan help
おわり
- 使用頻度高いartisanコマンドございましたらご教示ください!