DEV Community

Cover image for Notable: Polymorphic Notes for Any Laravel Model
Mohamed Said
Mohamed Said

Posted on

Notable: Polymorphic Notes for Any Laravel Model

Notable is a Laravel package that adds polymorphic note functionality to any Eloquent model.
Easily attach notes or comments to models with creator tracking, timestamps, and powerful query scopes.

This package is perfect for:

  • Internal comments
  • Audit logs
  • User feedback
  • Any kind of trackable textual annotations

✨ Key Features

  • πŸ”— Polymorphic Relationships – Attach notes to any Eloquent model.
  • πŸ‘€ Creator Tracking – Track who created each note (creator is also polymorphic!).
  • ⏰ Timestamps – Automatic created_at and updated_at tracking.
  • πŸ” Query Scopes – Powerful query methods for filtering and searching notes.
  • βš™οΈ Configurable – Customize table names through the config file.
  • πŸš€ Easy Integration – Simple trait-based implementation.
  • πŸ“¦ Laravel 10+ Ready – Built for modern Laravel applications.

πŸš€ Installation

Install via Composer:

composer require eg-mohamed/notable
Enter fullscreen mode Exit fullscreen mode

Publish and run the migrations:

php artisan vendor:publish --tag="notable-migrations"
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Optionally publish the config file:

php artisan vendor:publish --tag="notable-config"
Enter fullscreen mode Exit fullscreen mode

🎯 Quick Start

1️⃣ Add the Trait to a Model

use MohamedSaid\Notable\Traits\HasNotables;

class SupportTicket extends Model
{
    use HasNotables;
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ Start Adding Notes

// Customer adds a note
$ticket->addNote('Still experiencing the issue', $customer);

// Support agent responds
$ticket->addNote('Investigating the problem', $agent);

// Get conversation history
$conversation = $ticket->getNotesWithCreator();
Enter fullscreen mode Exit fullscreen mode

πŸ“š Common Operations

// Retrieve notes
$ticket->getNotes();
$ticket->getNotesToday();
$ticket->getNotesThisWeek();
$ticket->getNotesThisMonth();
$ticket->getNotesInRange('2024-01-01', '2024-12-31');

// Search notes by content
$ticket->searchNotes('error');

// Check if a model has notes
if ($ticket->hasNotes()) {
    echo "This ticket has {$ticket->notesCount()} notes.";
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Advanced Query Scopes

use MohamedSaid\Notable\Notable;

// Notes by specific creator
$notes = Notable::byCreator($user)->get();

// Notes without creator (system notes)
$systemNotes = Notable::withoutCreator()->get();

// Recent or older notes
$recentNotes = Notable::recent(7)->get();
$oldNotes = Notable::olderThan(30)->get();

// Date-based scopes
$todayNotes = Notable::today()->get();
$monthNotes = Notable::thisMonth()->get();
$rangeNotes = Notable::betweenDates('2024-01-01', '2024-12-31')->get();

// Search text
$searchResults = Notable::search('login')->get();
Enter fullscreen mode Exit fullscreen mode

πŸ—‚ Database Schema

Column Type Description
id bigint Primary key
note text The note content
notable_type varchar Polymorphic type (model class)
notable_id bigint Polymorphic ID (model ID)
creator_type varchar Creator polymorphic type (nullable)
creator_id bigint Creator polymorphic ID (nullable)
created_at timestamp Created timestamp
updated_at timestamp Updated timestamp

You can customize the table name in config/notable.php:

return [
    'table_name' => 'notables',
];
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Example Use Cases

User Activity Log

$user->addNote('Password changed', $admin);
$user->addNote('Logged in', $user);
Enter fullscreen mode Exit fullscreen mode

Order Tracking

$order->addNote('Order shipped', $system);
$order->addNote('Delivered successfully', $staff);
Enter fullscreen mode Exit fullscreen mode

Support Tickets

$ticket->addNote('User reported an error', $customer);
$ticket->addNote('Fix deployed', $developer);
Enter fullscreen mode Exit fullscreen mode

πŸ‘¨β€πŸ’» Contributing & Support

Top comments (0)