LoginSignup
3
1

More than 1 year has passed since last update.

Laravelでタスク管理のプログラムを作る

Last updated at Posted at 2021-12-12

こんにちわ。

web業界に入って3年が経過し、ふと過去を懐かしんだりする時がある中、
会社内でアドベントカレンダーなる企画に誘われました。
折角なので投稿してみたいと思います。

題目としてはLaravelでタスク管理のプログラムです。
一応初心者向け的な感じを想定しております。

時間があまりないので簡単に行きます。
前半と後半に投稿は分けます。
今回はLaravelのインストールと初期設定、DBの準備までを行います。


手順説明

  • 1. 環境準備
  • 2. 初期設定
  • 3. DBの準備 (最後にテストのためにちょっとだけ認証機能の実装があります)

1. 環境準備

投稿者環境
MaxOS BigSur、 mysql、 node

開発環境はdockerとか色々ありますが、あくまで初心者向けという事で、
「php artisan serve」で呼び出せる環境を使用します。
また事前に開発に必要なものとして「node」は必須なので準備します。
インストールはbrewで、つまづいたらネット上に大量にあるので割愛。
Laravelはnodeが必須になってきたので、初心者には少し敷居が高くなったかもしれません。
ちなみにエディタはVSコードがおすすめです。

Laravelのインストール
composer create-project laravel/laravel task_management --prefer-dist
※ --prefer-distで最新のバージョンがインストールされます。


2. 初期設定

環境の準備が終わったら、Laravelの設定をしていきます。envとか時間の設定とか。

envについて
初期から「.env」ファイルが配置されてると思いますが、なければ「.env.example」をコピーして作成できます。
色々設定項目がありますが基本的には以下の通りです。

APP_NAME=task
APP_ENV=local
APP_KEY=base64:XsZ+YZ/SM2s9q4KocBKXece00blCIYTETygQSLQ+X9w=
APP_DEBUG=true
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=task
DB_USERNAME=********
DB_PASSWORD=********

app.php
config配下に配置されている設定用のファイル
以下のようにしていきます。

'timezone' => 'Asia/Tokyo',
'locale' => 'ja',
'faker_locale' => 'ja_JP',

初期設定としては以上です。
dbのconfigもありますが今回は編集不要です。
envが正確に記載されていれば、envコマンドでenvファイルを読んでくれているので。


3. DBの準備

DBですがタスク管理に必要な要件を考えて、必要になるテーブルを作成していきます。
今回は自分向けのタスク管理を想定します。
「タスクの種別」、「タスクの状態」、「優先度」、「スケジュール」の概念があればある程度形になると思います。
なのでこれらを記録するテーブルと、概念を保持しておくマスタ系のテーブルを作成します。
上記の内、「スケジュール」に関してはタスクの記録テーブルに直接持たせます。
認証系のテーブルは初期から準備されているのでそれを利用します。(usersテーブル関係)
よって今回作成するテーブルは以下の通りです。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTasksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->tinyInteger('task_status_id')->nullable();
            $table->tinyInteger('task_type_id')->nullable();
            $table->tinyInteger('prioritie_id')->nullable();
            $table->dateTime('start')->nullable();
            $table->dateTime('end')->nullable();
            $table->text('memo')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tasks');
    }
}

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTaskTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('task_types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('task_types');
    }
}

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePrioritiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('priorities', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('priorities');
    }
}

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTaskStatusesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('task_statuses', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('task_statuses');
    }
}

マイグレーションとしては以上です。
ついでにseedファイルを作っておきます。マスタ用の初期データですね。

<?php
// これは実行したいシードを登録しておくファイルです。
namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
        $this->call(priorities_data_set::class);
        $this->call(task_statuses_data_set::class);
        $this->call(task_types_data_set::class);
    }
}

<?php

namespace Database\Seeders;

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

class priorities_data_set extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // truncateでデータリセット
        DB::table('priorities')->truncate();

        DB::table('priorities')->insert(['id' => '1', 'name' => '高', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('priorities')->insert(['id' => '2', 'name' => '中', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('priorities')->insert(['id' => '3', 'name' => '低', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
    }
}


<?php

namespace Database\Seeders;

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

class task_statuses_data_set extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // truncateでデータリセット
        DB::table('task_statuses')->truncate();

        DB::table('task_statuses')->insert(['id' => '1', 'name' => '未対応', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('task_statuses')->insert(['id' => '2', 'name' => '対応中', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('task_statuses')->insert(['id' => '3', 'name' => '保留', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('task_statuses')->insert(['id' => '4', 'name' => '完了', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
    }
}


<?php

namespace Database\Seeders;

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

class task_types_data_set extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // truncateでデータリセット
        DB::table('task_types')->truncate();

        DB::table('task_types')->insert(['id' => '1', 'name' => '打ち合わせ', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('task_types')->insert(['id' => '2', 'name' => '開発', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('task_types')->insert(['id' => '3', 'name' => '保守', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
        DB::table('task_types')->insert(['id' => '4', 'name' => 'その他', 'created_at' => DB::raw('NOW()'), 'updated_at' => DB::raw('NOW()')]);
    }
}

ここまでできたらマイグレーションを実行します。
php artisan migrate --seed
--seedで同時に初期データの投入を行います。

最後にマイグレーションが実行できたか、認証系の機能を動かしてテストしてみます。

※認証系の実装 上から順に実行
composer require laravel/ui
php artisan ui vue --auth
npm i
npm run dev

php artisan serveを実行し、ブラウザで以下を確認してみてください。
http://127.0.0.1:8000/
ページが表示されたら右上にログイン機能とユーザー登録の機能があるのでテストをお願いします。
問題なさそうなら今回はここまでです。

次回は中身の実装を行なっていきます。
最後まで読んでいただきありがとうございました。
お先に失礼します。

3
1
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
3
1