0
0

More than 1 year has passed since last update.

環境準備

Posted at

はじめに

環境準備であるが、DB操作の元になる箇所を重点に記載する。

前準備

・プロジェクト作成
(例)
composer create-project laravel/laravel laravel_test

・データベース作成

・.env修正
(例)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root

テーブル定義

テーブル名: project_works

カラム名:
employee_id : 社員ID
project_code : プロジェクトコード
work_date : 作業日
budget_actual_flag : 予定0,実績1
work_hours : 工数時間

テーブル作成

php artisan make:migration create_project_works_table --create=project_works

作成されたマイグレーションファイル(database/migrations直下に作成される)

    public function up(): void
    {
        Schema::create('project_works', function (Blueprint $table) {
            $table->id();
            $table->string('employee_id');
            $table->string('project_code');
            $table->date('work_date');
            $table->tinyInteger('budget_actual_flag');
            $table->decimal('work_hours', 5, 2);
            $table->timestamps();
        });
    }
php artisan migrate

データ作成

php artisan make:seeder ProjectWorksTableSeeder

database/seeders/ProjectWorksTableSeeder.phpが作成される。
データは、employee_idに対し複数のproject_codeがある場合、
各社員IDが1つのプロジェクトから5つのプロジェクトを割り当てる。
1日にできる労働時間は12時間まで、作業日数は複数日となるように
一人の社員が1日に扱うプロジェクト全体の労働時間の合計が12時間以内。
こんなの一人で作れない。

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ProjectWorksTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $startMonth = 6;  // Starting month
        $endMonth = 12;  // Ending month

        $data = [];

        for ($m = $startMonth; $m <= $endMonth; $m++) {
            // Additional data
            for ($i = 1; $i <= 22; $i++) {
                $empId = 'EMP' . str_pad($i, 3, '0', STR_PAD_LEFT);
                $numProjects = rand(1, 5);  // Generate random number of projects between 1 and 5

                for ($j = 1; $j <= $numProjects; $j++) {
                    $projectCode = 'PRJ' . str_pad($j, 3, '0', STR_PAD_LEFT);
                    $numDays = rand(1, 5);  // Generate random number of days between 1 and 5

                    for ($k = 0; $k < $numDays; $k++) {
                        $workDay = date('Y-m-d', strtotime("2023-{$m}-" . str_pad($i + $k, 2, '0', STR_PAD_LEFT)));
                        $workHours = 0;

                        // Calculate the total work hours already allocated to the employee on this day
                        $totalWorkHours = 0;
                        foreach ($data as $row) {
                            if ($row['employee_id'] == $empId && $row['work_date'] == $workDay) {
                                $totalWorkHours += $row['work_hours'];
                            }
                        }

                        $remainingHours = 12 - $totalWorkHours;  // Remaining hours the employee can work on this day

                        if ($remainingHours <= 0) continue;

                        if ($j != $numProjects) {
                            // If it's not the last project for the day, assign random hours, ensuring at least 1 hour remains for the last project
                            $workHours = rand(1, min($remainingHours, $remainingHours - ($numProjects - $j)));
                        } else {
                            // If it's the last project for the day, assign the remaining hours
                            $workHours = $remainingHours;
                        }

                        $data[] = [
                            'employee_id' => $empId,
                            'project_code' => $projectCode,
                            'work_date' => $workDay,
                            'work_hours' => $workHours,
                            'budget_actual_flag' => rand(0, 1)
                        ];
                    }
                }
            }
        }

        DB::table('project_works')->insert($data);
    }
}


シーダー実行

php artisan db:seed --class=ProjectWorksTableSeeder

やり直す場合はリセットしてから実行

php artisan migrate:fresh
php artisan db:seed --class=ProjectWorksTableSeeder
0
0
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
0
0