LoginSignup
1
1

More than 1 year has passed since last update.

【Laravel】Model/Migration/tinker/Controller

Last updated at Posted at 2023-02-19

Model

Laravelが採用するMVCモデルのうちの一つ

Model データベースとのやりとり
View 見た目
Controllwe 処理

Eloquent

<ORM/ORマッパー>
DBとのやりとりを他の言語で書ける
(Java、Ruby、Python同様)

モデルファイルの作成

ファイル作成コマンド

php artisan make:model Test

↓ファイルが自動生成される
image.png

※コントローラとマイグレーションも同時生成

php artisan make:model Test -mc

Mifration

DBテーブルの履歴管理

↓ファイル場所
image.png

モデルは単数形、マイグレーションは複製形で書く
→Laravelが自動判定

ファイル作成

php artisan make:migration create_tests_table

↓作成時の時間を含んだファイル名が自動生成される
image.png

ファイルの中身
image.png

↓書き足し

image.png

DBに適応させる

php artisan migrate

確認
image.png

image.png

その他のコマンド

テーブルを全て削除

php artisan migrate:fresh

ロールバックして再生成

php artisan migrate:refresh

tinker (DB簡易接続)

コマンド入力でデータを保存・閲覧できる

php artisan tinker

保存

// インスタンス化したものを変数へ代入
$test = new App\Models\Test;
$test->text = "hoge";
$test->save();

//全件表示
// Eloquent(ORマッパーの仕組み)
App\Models\Test::all();

image.png

Controller

Laravelが採用するMVCモデルのうちの一つ

Model データベースとのやりとり
View 見た目
Controllwe 処理

ファイル生成

php artisan make:controller TestController

↓自動生成される
image.png

1
1
1

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