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
This commit is contained in:
36
app/Models/Note.php
Normal file
36
app/Models/Note.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
|
||||||
|
class Note extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'notable_type',
|
||||||
|
'notable_id',
|
||||||
|
'content',
|
||||||
|
'author_user_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the parent notable model (Member, etc.)
|
||||||
|
*/
|
||||||
|
public function notable(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the user who authored this note
|
||||||
|
*/
|
||||||
|
public function author(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'author_user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
31
database/migrations/2026_02_13_120230_create_notes_table.php
Normal file
31
database/migrations/2026_02_13_120230_create_notes_table.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('notes', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user