# Architecture Patterns **Domain:** CRM/Admin Member Note Systems **Researched:** 2026-02-13 ## Recommended Architecture **Pattern:** Polymorphic Note System with Inline AJAX UI ``` ┌─────────────────────────────────────────────────────────────────┐ │ Member List Page (Blade) │ │ /admin/members │ │ │ │ ┌──────────────────────────────────────────────────────────┐ │ │ │ Member Row (Alpine.js Component) │ │ │ │ │ │ │ │ Name │ Status │ [Note Badge: 3] │ Actions │ │ │ │ ↓ (click to expand) │ │ │ │ ┌────────────────────────────────────────────────────┐ │ │ │ │ │ Expandable Note Panel (x-show) │ │ │ │ │ │ │ │ │ │ │ │ [Quick Add Form] │ │ │ │ │ │ Textarea: "新增備註..." │ │ │ │ │ │ [儲存] [取消] │ │ │ │ │ │ │ │ │ │ │ │ Note History (chronological, most recent first): │ │ │ │ │ │ • 2026-02-13 14:30 - Admin Name │ │ │ │ │ │ "Contacted about membership renewal..." │ │ │ │ │ │ • 2026-02-11 09:15 - Secretary Name │ │ │ │ │ │ "Follow-up needed on payment issue" │ │ │ │ │ └────────────────────────────────────────────────────┘ │ │ │ └──────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────┘ ↕ AJAX (Axios) ┌─────────────────────────────────────────────────────────────────┐ │ Laravel Backend API │ │ │ │ MemberNoteController │ │ ├─ index(Member $member): GET /admin/members/{id}/notes │ │ │ → Returns NoteResource::collection($member->notes) │ │ └─ store(Member $member, Request): POST /admin/members/{id}/notes│ │ → Validates, creates Note, logs audit, returns NoteResource │ └─────────────────────────────────────────────────────────────────┘ ↕ Eloquent ORM ┌─────────────────────────────────────────────────────────────────┐ │ Database Layer │ │ │ │ members table notes table (polymorphic) │ │ ├─ id ├─ id │ │ ├─ name ├─ notable_id ──┐ │ │ ├─ ... ├─ notable_type ──┼─> Polymorphic │ │ ├─ content │ Relationship │ │ ├─ user_id (author)│ │ │ ├─ created_at │ │ │ └─ updated_at ────┘ │ └─────────────────────────────────────────────────────────────────┘ ``` ### Component Boundaries | Component | Responsibility | Communicates With | |-----------|---------------|-------------------| | **Member List Page (Blade)** | Renders member table, embeds Alpine.js components per row | Alpine.js components | | **Alpine.js Note Component** | Manages note UI state (expanded, loading, form data), handles AJAX calls | MemberNoteController via Axios | | **MemberNoteController** | Validates requests, orchestrates note CRUD, returns JSON | Note model, AuditLogger | | **Note Model (Eloquent)** | Manages polymorphic relationship, defines fillable fields, relationships | members table, users table (author) | | **NoteResource** | Transforms Note model to consistent JSON structure | MemberNoteController | | **AuditLogger** | Records note creation events for compliance | Audit logs table | ### Data Flow **Creating a Note (Inline):** 1. User types note in textarea, clicks "儲存" 2. Alpine.js component calls `addNote()` method 3. Axios sends POST `/admin/members/{id}/notes` with CSRF token 4. MemberNoteController validates request (required content, max length) 5. Controller creates Note record with `notable_type='App\Models\Member'`, `notable_id={id}`, `user_id=auth()->id()` 6. AuditLogger logs event: `note_created` with member ID, author ID, note ID 7. Controller returns `{ data: NoteResource($note) }` (201 Created) 8. Alpine.js component prepends new note to `this.notes` array 9. UI updates instantly (no page reload), badge count increments **Loading Note History (Expandable):** 1. User clicks note badge (count) 2. Alpine.js component triggers `x-show` toggle (if first click, already loaded via `x-init`) 3. If not lazy-loaded: Axios sends GET `/admin/members/{id}/notes` 4. MemberNoteController queries `$member->notes()->with('author')->get()` 5. Controller returns `{ data: NoteResource::collection($notes) }` 6. Alpine.js component sets `this.notes = response.data.data` 7. UI renders note list with author names, timestamps (relative + tooltip absolute) ## Patterns to Follow ### Pattern 1: Polymorphic Relationship for Extensibility **What:** Use Laravel's polymorphic `morphMany`/`morphTo` to attach notes to any entity. **When:** Notes can eventually apply to Members, Issues, Payments, Approvals, etc. **Example:** \`\`\`php // Note model public function notable(): MorphTo { return $this->morphTo(); } // Member model public function notes(): MorphMany { return $this->morphMany(Note::class, 'notable')->latest(); } // Later: Issue model can add same relationship without schema changes public function notes(): MorphMany { return $this->morphMany(Note::class, 'notable')->latest(); } \`\`\` **Benefits:** - Single notes table for all entities (DRY) - Zero schema changes to add notes to new entities - Consistent query API: `$member->notes`, `$issue->notes` ### Pattern 2: Eager Loading with Count Aggregation **What:** Load note counts on member list without N+1 queries. **When:** Displaying note count badges on member list page. **Example:** \`\`\`php // In MemberController@index $members = Member::query() ->withCount('notes') // Adds 'notes_count' attribute ->paginate(15); // In Blade template @if ($member->notes_count > 0) {{ $member->notes_count }} @endif \`\`\` **Benefits:** - Single query for all note counts (vs. N queries for N members) - No caching needed for counts (eager load is fast) - Automatic when using Eloquent relationships ### Pattern 3: Alpine.js Component State Isolation **What:** Each member row has isolated Alpine.js state (not global). **When:** Multiple member rows on page, each with expandable notes. **Example:** \`\`\`html