食事管理機能の作成に着手します。
まずはデータベースの作成。
マイグレーションを作成します。
$ php artisan make:migration create_eating_management
class CreateEatingManagement extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('eating_managements', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->text('item');
$table->integer('protein');
$table->integer('ripid');
$table->integer('carbo');
$table->integer('calorie');
$table->timestamps();
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
Schema::create('eating_management_user', function (Blueprint $table) {
$table->integer('user_id')
->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->integer('eating_management_id')
->foreign('eating_management_id')
->references('id')->on('eating_managements')
->onDelete('cascade');
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
Schema::create('timezones', function (Blueprint $table) {
$table->increments('id');
$table->text('zone');
$table->timestamps();
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
Schema::create('eating_management_timezone', function (Blueprint $table) {
$table->integer('eating_management_id')
->foreign('eating_management_id')
->references('id')->on('eating_managements')
->onDelete('cascade');
$table->integer('timezone_id')
->foreign('timezone_id')
->references('id')->on('timezones')
->onDelete('cascade');
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('eating_management_timezone');
Schema::dropIfExists('timezones');
Schema::dropIfExists('eating_management_user');
Schema::dropIfExists('eating_managements');
}
}
必要な項目は、日付、時間帯、品目、タンパク質、脂質、炭水化物、カロリー。
時間帯は別テーブルで定義して、朝、昼、夜、間食から選択します。
時間帯は固定値なので、Seederで作成します。
$ php artisan make:seeder TimezoneSeeder
class TimezoneSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('timezones')->insert(['zone' => '朝',]);
DB::table('timezones')->insert(['zone' => '昼',]);
DB::table('timezones')->insert(['zone' => '夜',]);
DB::table('timezones')->insert(['zone' => '間食',]);
}
}
これらを実行して適用させます。
$ composer dump-autoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Package manifest generated successfully.
$ php artisan migrate
Migrating: 2020_07_10_092328_create_eating_management
Migrated: 2020_07_10_092328_create_eating_management (0.07 seconds)
$ php artisan db:seed --class=TimezoneSeeder
Database seeding completed successfully.
作成時間にNULLって入っているけど、まあいいか。