PHP code example of eg-mohamed / notable

1. Go to this page and download the library: Download eg-mohamed/notable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eg-mohamed / notable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MohamedSaid\Notable\Traits\HasNotables;

class User extends Model
{
    use HasNotables;
}

$user = User::find(1);

// Simple note
$user->addNote('User completed onboarding process');

// Note with creator
$admin = User::find(2);
$user->addNote('Account verified by admin', $admin);

// Get all notes
$notes = $user->getNotes();

// Check if model has notes
if ($user->hasNotes()) {
    echo "This user has {$user->notesCount()} notes";
}

// Basic note
$model->addNote('Something noteworthy happened');

// Note with creator (any model)
$model->addNote('Action performed', $currentUser);
$model->addNote('System generated note', $systemBot);

// Get all notes (newest first)
$notes = $model->getNotes();

// Get notes with creator information
$notesWithCreators = $model->getNotesWithCreator();

// Get latest note
$latestNote = $model->getLatestNote();

// Get notes by specific creator
$adminNotes = $model->getNotesByCreator($admin);

// Check if model has notes
$hasNotes = $model->hasNotes();

// Count notes
$count = $model->notesCount();

// Enhanced retrieval methods
$todayNotes = $model->getNotesToday();
$weekNotes = $model->getNotesThisWeek();
$monthNotes = $model->getNotesThisMonth();
$rangeNotes = $model->getNotesInRange('2024-01-01', '2024-12-31');
$searchResults = $model->searchNotes('error');

// Update a note
$model->updateNote($noteId, 'Updated note content');

// Delete a note
$model->deleteNote($noteId);

use MohamedSaid\Notable\Notable;

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

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

// Recent notes (last 7 days by default)
$recentNotes = Notable::recent()->get();
$lastMonth = Notable::recent(30)->get();

// Older notes (30+ days by default)  
$oldNotes = Notable::olderThan(30)->get();

// Date-based scopes
$todayNotes = Notable::today()->get();
$weekNotes = Notable::thisWeek()->get();
$monthNotes = Notable::thisMonth()->get();
$yearNotes = Notable::thisYear()->get();

// Date range filtering
$rangeNotes = Notable::betweenDates('2024-01-01', '2024-12-31')->get();

// Search note content
$searchResults = Notable::search('error')->get();
$containingText = Notable::containingText('login')->get();

// Combine scopes
$recentAdminNotes = Notable::byCreator($admin)
    ->thisMonth()
    ->search('important')
    ->orderBy('created_at', 'desc')
    ->get();


// config/notable.php
return [
    // Customize the table name
    'table_name' => 'notables',
];

class User extends Model 
{
    use HasNotables;
    
    public function logActivity(string $activity, ?Model $performer = null)
    {
        return $this->addNote("User activity: {$activity}", $performer);
    }
}

// Usage
$user->logActivity('Logged in', $user);
$user->logActivity('Password changed', $user);
$user->logActivity('Account suspended', $admin);

class Order extends Model
{
    use HasNotables;
    
    public function addStatusNote(string $status, ?User $updatedBy = null)
    {
        return $this->addNote("Order status changed to: {$status}", $updatedBy);
    }
}

// Usage
$order->addStatusNote('Processing', $staff);
$order->addStatusNote('Shipped', $system);
$order->addStatusNote('Delivered');

class SupportTicket extends Model
{
    use HasNotables;
}

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

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

// Get conversation history
$conversation = $ticket->getNotesWithCreator();

// Get the polymorphic relationship
$model->notables(); // Returns MorphMany relationship

// With eager loading
$users = User::with('notables.creator')->get();

// Load notes later
$user->load('notables');

$note = $model->getLatestNote();

$note->note;         // The note content
$note->notable;      // The parent model
$note->creator;      // The creator model (nullable)
$note->created_at;   // When it was created
$note->updated_at;   // When it was updated
bash
php artisan vendor:publish --tag="notable-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="notable-config"
bash
php artisan vendor:publish --tag="notable-config"