内容へ移動
fl8 Wiki
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
Dokuwiki.fl8.jp
»
11_php
»
02_framework
»
01_laravel
»
14 Laravel Model
トレース:
11_php:02_framework:01_laravel:14_laravel_model
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 14 Laravel Model ====== Modelとは、MVCアーキテクチャ(Model View Controller)の「M」にあたる部分で、主にデータベースとの連携を行います。 LaravelにおけるModelは、Eloquent(DBのデータを操作する実装)の機能とビジネスロジックを持ったクラスです。 基本的には1つのテーブルに1つのModelが存在します。 ===== 1. Model作成 ===== この場合だと、testテーブルを作成するという意味です。 <code> php artisan make:model Test $ ll app/Test.php -rw-r--r-- 1 matsui users 140 Jan 22 16:49 app/Test.php </code> ==== Modelの中で、テーブル名を指定する場合 ==== <code|app/Test.php > class Test extends Model { protected $table = 'test_tables'; } </code> ===== 2. Modelを使う ===== ==== Migrateを用意(テーブル作成) ==== ※make:migrationの決まりごと 小文字ならそのまま create_test_tables = test_tables 大文字ならスペースが入る createTestTables = test_tables <code> php artisan make:migration create_test_tables </code> === 例: === <code> $ php artisan make:migration create_test_tables Created Migration: 2019_01_22_042833_create_test_tables $ ll database/migrations/2019_01_22_042833_create_test_tables.php -rw-r--r-- 1 matsui users 598 Jan 22 13:28 database/migrations/2019_01_22_042833_create_test_tables.php </code> ==== Seedsを用意(初期データインサート) ==== <code> php artisan make:seed TestTableSeeder $ ll database/seeds/TestTableSeeder.php -rw-r--r-- 1 matsui users 296 Jan 22 14:11 database/seeds/TestTableSeeder.php </code> <code|database/seeds/TestTableSeeder.php> public function run() { DB::table('test_tables')->insert([ 'name' => 'default_user', ]); </code> <code|database/seeds/DatabaseSeeder.php> public function run() { $this->call(TestTableSeeder::class); } </code> ==== MigrateとSeeds実行 ==== <code> php artisan migrate php artisan db:seed </code> db:seed を実行すると、database/seeds/DatabaseSeeder.phpが実行される。 Migrateと同時にSeedsも実行する場合 <code> php artisan migrate --seed </code> === ロールバック === 最後の1個をロールバック <code> php artisan migrate:rollback --step=1 </code> == 最後の1個だけ再度マイグレーションしたい場合 == 下記から必要なマイグレーション以外移動させておいてから、再度マイグレーション実施 database/migrations/ === マイグレーション状態確認 === <code> $ php artisan migrate:status +------+-----------------------------------------------------------------+-------+ | Ran? | Migration | Batch | +------+-----------------------------------------------------------------+-------+ | Yes | 2014_10_12_000000_create_users_table | 1 | | Yes | 2014_10_12_100000_create_password_resets_table | 1 | | Yes | 2016_01_01_000000_add_voyager_user_fields | 1 | | Yes | 2016_01_01_000000_create_data_types_table | 1 | | Yes | 2016_01_01_000000_create_pages_table | 16 | </code> ===== コントローラ作成 ===== <code> $ php artisan make:controller TestController $ ll app/Http/Controllers/TestController.php -rw-r--r-- 1 matsui users 121 Jan 22 14:54 app/Http/Controllers/TestController.php </code> {{tag>laravel php}}
11_php/02_framework/01_laravel/14_laravel_model.txt
· 最終更新: 2021/06/24 07:09 by
matsui
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ