Skip to content

Commit 58075fb

Browse files
authored
Update database documentation (#127)
* fix tables relationship * add item for contributing * fix database tables desc * fix spatie table name and update docs * fix spatie table name of database docs * fix er diagram team_roles table not relation to teams and roles tables and module_permissions not relation to modules and permissions tables * add Docker Setup Image info * fix seeders use update and sync method will overwirte live up to date data and let seeders code cleaning * add more tode item to readme * fix coding style * fix duplicate same role
1 parent a5beddb commit 58075fb

File tree

9 files changed

+214
-235
lines changed

9 files changed

+214
-235
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,6 @@ Please read database/README.md for database setup and details.
168168
- add shop order show page
169169
- Add admin exchange and refund for shop orders
170170

171-
#### stage 7 analytics (coming soon)
171+
#### stage 7 analytics (coming soon)
172+
173+
#### stage 8 contest (coming soon)

config/permission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
* basic default value but you may easily change it to any table you like.
6969
*/
7070

71-
'role_has_permissions' => 'team_roles_has_permissions',
71+
'role_has_permissions' => 'team_roles_has_module_permissions',
7272
],
7373

7474
'column_names' => [

database/README.md

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ mysql:
6666
MYSQL_ROOT_PASSWORD: password
6767
```
6868
69+
### Docker Setup Image
70+
71+
Please read laravel sail official document
72+
6973
---
7074
7175
## Entity Relationship Diagram (ERD)
@@ -81,7 +85,6 @@ erDiagram
8185
users }o--|| passport_types : belongs_to
8286
users ||--o{ user_login_logs : has
8387
users ||--o{ admission_test_orders : creates
84-
users ||--o{ contact_has_verifications : creates
8588
users ||--o{ reset_password_logs : has
8689
users ||--o{ sessions : has
8790

@@ -107,30 +110,23 @@ erDiagram
107110

108111
%% Payment & Products
109112
admission_test_orders }o--|| users : ordered_by
110-
admission_test_orders }o--|| stripe_customers : "via (polymorphic)"
111113
admission_test_orders }o--|| other_payment_gateways : "via (polymorphic)"
112114
admission_test_products ||--o{ admission_test_prices : has
113115

114116
%% Stripe Integration
115117
stripe_customers }o--|| users : "for (polymorphic)"
116118

117119
%% Permission System (Spatie)
118-
permissions ||--o{ module_permissions : has
119-
modules ||--o{ module_permissions : has
120-
module_permissions }o--|| permissions : belongs_to
121-
module_permissions }o--|| modules : belongs_to
122-
roles ||--o{ team_roles : has
123-
teams ||--o{ team_roles : has
124-
team_roles }o--|| roles : belongs_to
125-
team_roles }o--|| roles : belongs_to
120+
team_roles }o--|| teams : has
121+
team_roles }o--|| roles : has
122+
module_permissions }o--|| modules : has
123+
module_permissions }o--|| permissions : has
126124
teams }o--|| team_types : has_type
127125

128126
%% Permission Pivot Tables
129127
users }o--o{ module_permissions : model_has_module_permissions
130128
users }o--o{ team_roles : model_has_team_roles
131-
roles }o--o{ module_permissions : team_roles_has_permissions
132-
teams }o--o{ roles : team_roles
133-
modules }o--o{ permissions : module_permissions
129+
team_roles }o--o{ module_permissions : team_roles_has_module_permissions
134130

135131
%% CMS & Navigation
136132
site_pages ||--o{ site_contents : has
@@ -505,7 +501,7 @@ Primary user table storing user account information.
505501
- `id` - Primary key
506502
- `username` - Unique username (max 320 chars)
507503
- `password` - Hashed password
508-
- `family_name`, `middle_name`, `given_name` - User's full name
504+
- `family_name`, `middle_name`, `given_name` - User's adorned name
509505
- `gender_id` - Foreign key to `genders` table
510506
- `passport_type_id` - Foreign key to `passport_types` table
511507
- `passport_number` - User's identification number
@@ -515,9 +511,9 @@ Primary user table storing user account information.
515511
**Relationships:**
516512

517513
- `belongsTo`: Gender, PassportType
518-
- `hasMany`: UserHasContact, UserLoginLog, AdmissionTestOrder
514+
- `hasMany`: UserLoginLog, UserHasContact, ResetPasswordLog, AdmissionTestOrder
519515
- `hasOne`: Member
520-
- `belongsToMany`: AdmissionTest (via proctors/candidates), Permissions, Roles
516+
- `belongsToMany`: AdmissionTest (via proctors/candidates), ModulePermission, TeamRole
521517

522518
#### `members`
523519

@@ -720,9 +716,9 @@ Categories of teams.
720716

721717
#### Pivot Tables:
722718

723-
- `model_has_permissions` - Polymorphic: User/Model → Permissions
724-
- `model_has_roles` - Polymorphic: User/Model → Roles
725-
- `role_has_permissions` - Role → Permissions
719+
- `model_has_module_permissions` - Polymorphic: User/Model → Module Permissions
720+
- `model_has_team_roles` - Polymorphic: User/Model → Team Roles
721+
- `team_role_has_module_permissions` - Team Role → Module Permissions
726722
- `team_roles` - Team → Roles (custom)
727723
- `module_permissions` - Module → Permissions (custom)
728724

@@ -847,17 +843,19 @@ Area
847843
### Permission System
848844

849845
```
850-
Module
851-
└─ hasMany: Permission
852-
└─ belongsToMany: Role
853-
854-
Team
855-
├─ belongsTo: TeamType
856-
└─ belongsToMany: Role
857-
858-
User
859-
├─ hasMany: Permission (via model_has_permissions)
860-
└─ hasMany: Role (via model_has_roles)
846+
ModulePermission(模封權限)
847+
├─ belongsTo: Module(模封)
848+
├─ belongsTo: Permission(權限)
849+
└─ belongsToMany: TeamRole(團隊角色)
850+
851+
TeamRole(團隊角色)
852+
├─ belongsTo: Role(角色)
853+
└─ belongsTo: Team(團隊)
854+
└─ belongsTo: TeamType(團隊類型)
855+
856+
User(用戶)
857+
├─ hasMany: ModulePermission(模封權限)通過 model_has_module_permissions
858+
└─ hasMany: TeamRole(團隊角色)通過 model_has_team_roles
861859
```
862860

863861
---
@@ -1283,9 +1281,10 @@ When adding new migrations:
12831281
12841282
1. Follow Laravel naming conventions: `YYYY_MM_DD_HHMMSS_create_table_name.php`
12851283
2. Include both `up()` and `down()` methods
1286-
3. Add foreign key constraints where appropriate
1287-
4. Update this README with new table documentation
1288-
5. Create corresponding factory if needed for testing
1284+
3. Strongly recommend using command of `php artisan make:migration create_{create table name}_table` to generate the migration file
1285+
4. Add foreign key constraints where appropriate
1286+
5. Update this README with new table documentation
1287+
6. Create corresponding factory if needed for testing
12891288
12901289
---
12911290

database/readme_zh_hk.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ mysql:
6868
MYSQL_ROOT_PASSWORD: password
6969
```
7070
71+
### Docker 映像建立
72+
73+
Please read laravel sail official document
74+
7175
---
7276
7377
## 實體關係圖(ERD)
@@ -76,63 +80,55 @@ mysql:
7680
7781
```mermaid
7882
erDiagram
79-
%% 核心用戶管理
83+
%% 用戶個人資料
8084
users ||--o{ user_has_contacts : has
8185
users ||--|| members : "can be"
8286
users }o--|| genders : belongs_to
8387
users }o--|| passport_types : belongs_to
8488
users ||--o{ user_login_logs : has
8589
users ||--o{ admission_test_orders : creates
86-
users ||--o{ contact_has_verifications : creates
8790
users ||--o{ reset_password_logs : has
8891
users ||--o{ sessions : has
8992

9093
%% 用戶聯繫方式
9194
user_has_contacts ||--o{ contact_has_verifications : has
9295

93-
%% 成員與地址
96+
%% 會員個人資料補充
9497
members }o--|| addresses : has
9598
addresses }o--|| districts : located_in
9699
districts }o--|| areas : belongs_to
97100

98-
%% 資格試
101+
%% 入會試
99102
admission_tests }o--|| admission_test_types : has_type
100103
admission_tests }o--o| locations : held_at
101104
admission_tests }o--o| addresses : held_at
102105
admission_tests ||--o{ admission_test_has_candidate : has
103106
admission_tests ||--o{ admission_test_has_proctor : has
104107

105-
%% 資格試關係
108+
%% 入會試關係
106109
admission_test_has_candidate }o--|| users : candidate
107110
admission_test_has_candidate }o--o| admission_test_orders : paid_via
108111
admission_test_has_proctor }o--|| users : proctor
109112

110-
%% 支付與產品
113+
%% 入會試支付與產品
111114
admission_test_orders }o--|| users : ordered_by
112-
admission_test_orders }o--|| stripe_customers : "via (polymorphic)"
113115
admission_test_orders }o--|| other_payment_gateways : "via (polymorphic)"
114116
admission_test_products ||--o{ admission_test_prices : has
115117

116118
%% Stripe 集成
117119
stripe_customers }o--|| users : "for (polymorphic)"
118120

119121
%% 權限系統(Spatie)
120-
permissions ||--o{ module_permissions : has
121-
modules ||--o{ module_permissions : has
122-
module_permissions }o--|| permissions : belongs_to
123-
module_permissions }o--|| modules : belongs_to
124-
roles ||--o{ team_roles : has
125-
teams ||--o{ team_roles : has
126-
team_roles }o--|| roles : belongs_to
127-
team_roles }o--|| roles : belongs_to
122+
team_roles }o--|| teams : has
123+
team_roles }o--|| roles : has
124+
module_permissions }o--|| modules : has
125+
module_permissions }o--|| permissions : has
128126
teams }o--|| team_types : has_type
129127

130128
%% 權限樞紐表
131129
users }o--o{ module_permissions : model_has_module_permissions
132130
users }o--o{ team_roles : model_has_team_roles
133-
roles }o--o{ module_permissions : team_roles_has_permissions
134-
teams }o--o{ roles : team_roles
135-
modules }o--o{ permissions : module_permissions
131+
team_roles }o--o{ module_permissions : team_roles_has_module_permissions
136132

137133
%% CMS 與導航
138134
site_pages ||--o{ site_contents : has
@@ -517,7 +513,7 @@ erDiagram
517513
**關係:**
518514

519515
- `belongsTo`:性別、護照類型
520-
- `hasMany`:用戶聯繫方式、用戶登錄日誌、資格試訂單
516+
- `hasMany`:用戶聯繫方式、用戶登錄日誌、用戶密碼重置紀錄、資格試訂單
521517
- `hasOne`:成員
522518
- `belongsToMany`:資格試(通過監考員/考生)、權限、角色
523519

@@ -722,9 +718,9 @@ Stripe 客戶映射的多態表。
722718

723719
#### 樞紐表:
724720

725-
- `model_has_permissions` - 多態:用戶/模型 → 權限
726-
- `model_has_roles` - 多態:用戶/模型 → 角色
727-
- `role_has_permissions` - 角色權限
721+
- `model_has_module_permissions` - 多態:用戶/模型 → 模塊權限
722+
- `model_has_team_roles` - 多態:用戶/模型 → 團隊角色
723+
- `team_roles_has_module_permissions` - 團隊角色模塊權限
728724
- `team_roles` - 團隊 → 角色(自定義)
729725
- `module_permissions` - 模塊 → 權限(自定義)
730726

@@ -815,6 +811,7 @@ Laravel 遷移跟蹤表。
815811
User(用戶)
816812
├─ hasMany: UserHasContact(用戶聯繫方式)
817813
├─ hasMany: UserLoginLog(用戶登錄日誌)
814+
├─ hasMany: ResetPasswordLog(用戶登錄日誌)
818815
├─ hasMany: AdmissionTestOrder(資格試訂單)
819816
├─ hasOne: Member(成員)
820817
├─ belongsTo: Gender(性別)
@@ -849,17 +846,19 @@ Area(區域)
849846
### 權限系統
850847

851848
```
852-
Module(模塊)
853-
└─ hasMany: Permission(權限)
854-
└─ belongsToMany: Role(角色)
849+
ModulePermission(模封權限)
850+
├─ belongsTo: Module(模封)
851+
├─ belongsTo: Permission(權限)
852+
└─ belongsToMany: TeamRole(團隊角色)
855853
856-
Team(團隊)
857-
├─ belongsTo: TeamType(團隊類型)
858-
└─ belongsToMany: Role(角色)
854+
TeamRole(團隊角色)
855+
├─ belongsTo: Role(角色)
856+
└─ belongsTo: Team(團隊)
857+
└─ belongsTo: TeamType(團隊類型)
859858
860859
User(用戶)
861-
├─ hasMany: Permission(權限)通過 model_has_permissions
862-
└─ hasMany: Role(角色)通過 model_has_roles
860+
├─ hasMany: ModulePermission(模封權限)通過 model_has_module_permissions
861+
└─ hasMany: TeamRole(團隊角色)通過 model_has_team_roles
863862
```
864863

865864
---
@@ -1285,9 +1284,10 @@ $candidates = AdmissionTest::find($testId)
12851284
12861285
1. 遵循 Laravel 命名規範:`YYYY_MM_DD_HHMMSS_create_table_name.php`
12871286
2. 包括 `up()``down()` 方法
1288-
3. 適當地添加外鍵約束
1289-
4. 使用新表文檔更新此 README
1290-
5. 如果需要測試,創建相應的工廠
1287+
3. 強烈建議使用`php artisan make:migration create_{create table name}_table`指令生成遷移檔案
1288+
4. 適當地添加外鍵約束
1289+
5. 使用新表請更新此文檔(中及英版本)
1290+
6. 如果需要測試,創建相應的工廠
12911291
12921292
---
12931293

database/seeders/GenderSeeder.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@
55
use App\Models\Gender;
66
use Illuminate\Database\Seeder;
77

8-
/**
9-
* This seeder populates the genders table with predefined data as follows:
10-
* | id | name | created_at | updated_at |
11-
* | --- | ------ | ---------- | ---------- |
12-
* | 1 | Male | ... | ... |
13-
* | 2 | Female | ... | ... |
14-
*/
158
class GenderSeeder extends Seeder
169
{
1710
public function run(): void
1811
{
19-
$rows = [
20-
['name' => 'Male'],
21-
['name' => 'Female'],
22-
];
23-
foreach ($rows as $row) {
24-
Gender::firstOrCreate($row);
12+
$names = ['Male', 'Female'];
13+
foreach ($names as $name) {
14+
Gender::create(['name' => $name]);
2515
}
2616
}
2717
}

database/seeders/OtherPaymentGatewaySeeder.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,26 @@ class OtherPaymentGatewaySeeder extends Seeder
1919
{
2020
public function run()
2121
{
22-
$gateway = OtherPaymentGateway::firstOrCreate(['name' => 'Cash']);
23-
$gateway->update([
24-
'display_order' => 0,
25-
'is_active' => true,
26-
]);
27-
$gateway = OtherPaymentGateway::firstOrCreate(['name' => 'Faster Payment System']);
28-
$gateway->update([
29-
'display_order' => 1,
30-
'is_active' => true,
31-
]);
32-
$gateway = OtherPaymentGateway::firstOrCreate(['name' => 'PayMe']);
33-
$gateway->update([
34-
'display_order' => 2,
35-
'is_active' => true,
36-
]);
22+
OtherPaymentGateway::firstOrCreate(
23+
['name' => 'Cash'],
24+
[
25+
'display_order' => 0,
26+
'is_active' => true,
27+
]
28+
);
29+
OtherPaymentGateway::firstOrCreate(
30+
['name' => 'Faster Payment System'],
31+
[
32+
'display_order' => 1,
33+
'is_active' => true,
34+
]
35+
);
36+
OtherPaymentGateway::firstOrCreate(
37+
['name' => 'PayMe'],
38+
[
39+
'display_order' => 2,
40+
'is_active' => true,
41+
]
42+
);
3743
}
3844
}

database/seeders/PassportTypeSeeder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class PassportTypeSeeder extends Seeder
1818
{
1919
public function run(): void
2020
{
21-
$rows = [
22-
['name' => 'China Identity Card'],
23-
['name' => 'Hong Kong Identity Card'],
24-
['name' => 'Macau Identity Card'],
21+
$names = [
22+
'China Identity Card',
23+
'Hong Kong Identity Card',
24+
'Macau Identity Card',
2525
];
26-
foreach ($rows as $row) {
27-
PassportType::firstOrCreate($row);
26+
foreach ($names as $name) {
27+
PassportType::firstOrCreate(['name' => $name]);
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)