ひな型作り
- プロジェクトを作成
curl -s https://laravel.build/example-app | bash
- タイムゾーン
config/app.php
//======== 前略 ======== |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. We have gone | ahead and set this to a sensible default for you out of the box. | */ - 'timezone' => 'UTC', + 'timezone' => 'Asia/Tokyo', //======== 後略 ========
データベースを作成
- phpMyAdminを追加
docker-compose.yml
phpmyadmin: image: phpmyadmin/phpmyadmin links: - mysql:mysql ports: - 8080:80 environment: #PMA_USER: "${DB_USERNAME}" #PMA_PASSWORD: "${DB_PASSWORD}" PMA_HOST: mysql networks: - sail
phpMyAdmin上で新規データベースを作成
設定ファイル
.env
- DB_DATABASE=laravel +DB_DATABASE=samuraimart
カテゴリのモデルとマイグレーションファイルを生成
sail artisan make:model Category -m
-m
オプションはマイグレーションファイルも同時に作成するオプション
- マイグレーションファイルを編集
カラム名 | 説明 | Laravel関数 | MySQLのデータ型 |
---|---|---|---|
name | カテゴリ名 | string | varchar(255) |
description | カテゴリの説明文 | text | text |
major_category_name | カテゴリの大分類 | string | varchar(255) |
database\migrations\XXXX_XX_XX_XXXXXX_create_categories_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); + $table->string('name'); + $table->text('description'); + $table->string('major_category_name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } };
- マイグレーションを実行
sail artisan migrate
Tinkerでカテゴリーを追加
sail artisan tinker
Tinkerを起動
Psy Shell v0.11.8 (PHP 8.1.6 — cli) by Justin Hileman >>>
例えば以下のような、Categoryモデルのデータを取得するコードを、コントローラーに実装する前に確認できます。
App\Models\Category::all()
[!] Aliasing 'Category' to 'App\Models\Category' for this Tinker session. => Illuminate\Database\Eloquent\Collection {#4284 all: [], }
Tinkerを使ってカテゴリを作成
$category = new App\Models\Category()
>> => App\Models\Category {#3566}
$category->major_category_name = "Food"
>> => "Food"
$category->name = "meat"
>> => "meat"
$category->description = "description for meat."
>> => "description for meat."
$category->save()
>> => true
データが作成されているかCategoryモデルのデータを取得するコードを実行
App\Models\Category::all()
=> Illuminate\Database\Eloquent\Collection {#4500 all: [ App\Models\Category {#4498 id: 1, name: "meat", description: "description for meat.", major_category_name: "Food", created_at: "2022-08-06 21:52:58", updated_at: "2022-08-06 21:52:58", }, ], }
- 終了
exit
シーダーでカテゴリを追加
php artisan make:seeder CategoriesTableSeeder
>> INFO Seeder created successfully.
database\seeders\CategoriesTableSeeder.php
<?php namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; +use App\Models\Category; class CategoriesTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { - // + $major_category_names = [ + '本', 'コンピュータ', 'ディスプレイ' + ]; + + $book_categories = [ + 'ビジネス', '文学・評論', '人文・思想', 'スポーツ', + 'コンピュータ・IT', '資格・検定・就職', '絵本・児童書', '写真集', + 'ゲーム攻略本', '雑誌', 'アート・デザイン', 'ノンフィクション' + ]; + + $computer_categories = [ + 'ノートPC', 'デスクトップPC', 'タブレット' + ]; + + $display_categories = [ + '19~20インチ', 'デスクトップPC', 'タブレット' + ]; + + foreach ($major_category_names as $major_category_name) { + if ($major_category_name == '本') { + foreach ($book_categories as $book_category) { + Category::create([ + 'name' => $book_category, + 'description' => $book_category, + 'major_category_name' => $major_category_name + ]); + } + } + + if ($major_category_name == 'コンピュータ') { + foreach ($computer_categories as $computer_category) { + Category::create([ + 'name' => $computer_category, + 'description' => $computer_category, + 'major_category_name' => $major_category_name + ]); + } + } + + if ($major_category_name == 'ディスプレイ') { + foreach ($display_categories as $display_category) { + Category::create([ + 'name' => $display_category, + 'description' => $display_category, + 'major_category_name' => $major_category_name + ]); + } + } + } } }
作成したシーダーファイルを読み込むためのコマンドを実行
sail composer dump-autoload
全テーブルを削除
php artisan migrate:fresh
シーダーでカテゴリーデータを作成
sail artisan db:seed --class=CategoriesTableSeeder
phpMyAdminでテーブルにデータが追加されているか確認する。