家庭任務平台中主要使用了使用者,計畫,任務,活動,個人資料這五者的關係分別為:
除了Laravel本身即創建的資料表(包含users),我們需要自己另外建立下面五個資料表:
schema::create('projects', function (Blueprint $table) {
$table->id();
$table->foreignId('owner_id');
$table->string('title');
$table->text('description');
$table->text('note')->nullable();
$table->timestamps();
$table->foreign('owner_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->foreignId('project_id')
->constrained('projects')
->onDelete('cascade');
$table->foreignId('assignee_id')
->constrained('users')
->onDelete('cascade');
$table->text('body');
$table->dateTime('start');
$table->dateTime('due');
$table->boolean('completed')->default(false);
$table->timestamps();
Schema::create('activities', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->nullableMorphs('recordable');
$table->text('description');
$table->json('changes')->nullable();
$table->timestamps();
});
Schema::create('member_project', function (Blueprint $table) {
$table->primary(['member_id','project_id']);
$table->foreignId('member_id');
$table->foreignId('project_id');
$table->timestamps();
$table->foreign('member_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('project_id')
->references('id')
->on('projects')
->onDelete('cascade');
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->string('avatar');
$table->unique('user_id');
$table->timestamps();
});
明天則是這些Model間關係的程式碼,之後便會進入Eloquent ORM系列二:Model與Model關係的操作的整理,了解Eloquent ORM為Model提供了哪些關係。