1
1

More than 3 years have passed since last update.

PHP Laravel Artisanコマンドのメモ

Posted at

はじめに

Laravelの開発で色々なArtisanコマンドがあるため、よく使用するものを、

ここにメモとして残して、追加で必要なものは更新していこうと思う

Artisanコマンドとは

  • PHPのフレームワークであるLaravelのコマンドのこと
  • Artisanコマンドを使用することでModelやControllerを作成することができる

Artisanでのコマンドリスト

ターミナル
php artisan list

Artisanでのコマンドのリストをターミナルに表示できる

サーバの起動コマンド

cd コマンドでLaravelのプロジェクトがあるとこまで移動しておくこと

ターミナル
php artisan serve

87f3108030716942f8db77a22d768b50.png

記載されているURL:http://〇〇〇.〇〇〇.〇〇〇.〇〇〇:8000

このURLにアクセスすればサーバーが立ち上がっているか確認できる

画像コマンドのミスがありますが見逃してください・・・

tinker起動

Laravelの機能を試せるREPLが起動します。

ターミナル
php artisan tinker

tinker終了コマンド

ターミナル
>>> exit
Exit:  Goodbye

マイグレーション

マイグレーションファイルの作成

マイグレーションファイルとは、Laravelからデータベースのテーブルを管理

するためのファイルのこと

ターミナル
php artisan make:migration create_messages_table --create=messages

php artisan make:migration //マイグレーションファイル作成のためのコマンド
create_messages_table //マイグレーションファイルの名前 名前を見て何をするかわかる名前にする

--create=messages //テーブル名を決定
--create=messages ← すべて小文字で複数形

マイグレーションのロールバックのコマンド

マイグレーションファイルをある地点まで戻すコマンド

ターミナル
php artisan migrate:rollback

マイグレーションの実行

ターミナル
php artisan migrate

マイグレーションの実行状況の確認

ターミナル
php artisan migrate:status

モデルの作成

Modelはapp/ 直下に生成、配置される

ターミナル
php artisan make:model モデル名 ← 頭が大文字で単数形であること!

Controllerの生成

ターミナル
php artisan make:controller コントローラ名

RESTful Resource Controllerの生成

例文のルーティング

routes/web.php
<?php

Route::resource('messages', 'MessagesController');

この時のコントローラの生成コマンド

Routerに対応したController名をつけないとルーティングされないので注意

ターミナル
php artisan make:controller MessagesController --resource

参考にさせていただいたサイト

【Laravel】Artisanコマンド基礎

1
1
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
1
1