From f2912badfa5f6ceb1166ad90526981ee4820c9c5 Mon Sep 17 00:00:00 2001 From: gbanyan Date: Fri, 13 Feb 2026 12:03:10 +0800 Subject: [PATCH] feat(01-01): create notes table and Note model with polymorphic relationships - Add notes migration with polymorphic columns (notable_type, notable_id) - Add foreign key to users table for author tracking - Add indexes on composite (notable_type, notable_id) and created_at - Create Note model with morphTo and belongsTo relationships --- app/Models/Note.php | 36 +++++++++++++++++++ .../2026_02_13_120230_create_notes_table.php | 31 ++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 app/Models/Note.php create mode 100644 database/migrations/2026_02_13_120230_create_notes_table.php diff --git a/app/Models/Note.php b/app/Models/Note.php new file mode 100644 index 0000000..bbfb390 --- /dev/null +++ b/app/Models/Note.php @@ -0,0 +1,36 @@ +morphTo(); + } + + /** + * Get the user who authored this note + */ + public function author(): BelongsTo + { + return $this->belongsTo(User::class, 'author_user_id'); + } +} diff --git a/database/migrations/2026_02_13_120230_create_notes_table.php b/database/migrations/2026_02_13_120230_create_notes_table.php new file mode 100644 index 0000000..8b5da08 --- /dev/null +++ b/database/migrations/2026_02_13_120230_create_notes_table.php @@ -0,0 +1,31 @@ +id(); + $table->morphs('notable'); // Creates notable_type, notable_id with composite index + $table->longText('content'); + $table->foreignId('author_user_id')->constrained('users')->cascadeOnDelete(); + $table->timestamps(); + $table->index('created_at'); // For chronological sorting + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('notes'); + } +};