PHP code example of paulohps / laravel-timeline

1. Go to this page and download the library: Download paulohps/laravel-timeline 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/ */

    

paulohps / laravel-timeline example snippets


namespace App\Timeline;

use Paulohps\Timeline\Events\TimelineEvent;

class Login extends TimelineEvent
{
    public function label(): string
    {
        return __('Logged in');
    }
}

namespace App\Timeline;

use Paulohps\Timeline\Events\TimelineEvent;
use Paulohps\Timeline\Models\TimelineEntry;

class OrderShipped extends TimelineEvent
{
    // Stable storage/URL key. Defaults to the snake-cased class
    // basename ('order_shipped'), so this override is optional.
    public static function key(): string
    {
        return 'order_shipped';
    }

    public function label(): string
    {
        return __('Order shipped');
    }

    // Shown next to on/off switches on a settings page, if you build one.
    public function description(): string
    {
        return __('When an order leaves the warehouse.');
    }

    // Inline HTML for the marker: an SVG, an emoji, an icon-font tag.
    // Return null (the default) for a plain dot.
    public function icon(): ?string
    {
        return '<svg viewBox="0 0 24 24">…</svg>';
    }

    // Marker color token → styled by .lt-item__marker--{token}.
    // Shipped tokens: default, primary, success, info, warning, danger.
    public function color(): string
    {
        return 'success';
    }

    // Secondary line under the title. Defaults to the request context
    // captured at record time; subjectTitle() prefers the metadata
    // 'title' snapshot and falls back to $entry->subject->title.
    public function detail(TimelineEntry $entry): ?string
    {
        return $this->subjectTitle($entry);
    }

    // Dropdown links rendered on the entry (plain <details>, no JS).
    public function navigation(TimelineEntry $entry): array
    {
        return [
            ['label' => __('View order'), 'href' => route('orders.show', $entry->subject_id)],
        ];
    }
}

'events' => [
    App\Timeline\Login::class,
    App\Timeline\OrderShipped::class,
],

use Paulohps\Timeline\Facades\Timeline;

Timeline::register([
    App\Timeline\Login::class,
    App\Timeline\OrderShipped::class,
]);

use Paulohps\Timeline\Concerns\HasTimeline;

class User extends Authenticatable
{
    use HasTimeline;
}

// Via the trait — accepts an instance, class name or registered key:
$user->recordTimelineEvent(new Login);
$user->recordTimelineEvent(OrderShipped::class, $order, ['title' => $order->number]);
$user->recordTimelineEvent('order_shipped', $order);

// Via the event itself:
(new OrderShipped)->record($user, $order);

// Via the facade:
Timeline::record($user, 'login');

$user->timelineEntries; // MorphMany of TimelineEntry

$user->recordTimelineEvent('order_shipped', $order, [
    'title' => $order->number,   // snapshot used by subjectTitle()
    'carrier' => 'DHL',
]);

use Illuminate\Http\Request;
use Paulohps\Timeline\Facades\Timeline;

Timeline::resolveContextUsing(function (?Request $request) {
    // $request is null outside HTTP (queued jobs, artisan commands).
    return [
        'ip' => $request?->ip(),
        'browser' => ...,
        'os' => ...,
    ];
});

// config/timeline.php
'enabled' => [
    'login' => false, // stop recording logins; everything else stays on
],

use Paulohps\Timeline\Models\TimelineEntry;

TimelineEntry::forCauser($user)->ofType('login')->count();
TimelineEntry::forCauser($team)->ofType(['login', 'logout'])->latest()->get();

$user->timelineEntries()->ofType('login')->first(); // last login

public function render(TimelineEntry $entry): View
{
    return view('timeline.order-shipped', ['entry' => $entry]);
}

'date_format' => 'F j',  // day-group labels (Today/Yesterday take precedence)
'time_format' => 'H:i',  // per-entry timestamp

// routes/console.php — with config('timeline.prune_days') set:
Schedule::command('timeline:prune')->daily();

class ActivityEntry extends \Paulohps\Timeline\Models\TimelineEntry
{
    // scopes, accessors, pruning, whatever you need
}

// config/timeline.php
'model' => App\Models\ActivityEntry::class,

use Paulohps\Timeline\Models\TimelineEntry;

TimelineEntry::factory()
    ->type(OrderShipped::class)      // or ->type('order_shipped')
    ->causedBy($user)
    ->about($order)
    ->withMetadata(['carrier' => 'DHL'])
    ->create();
bash
php artisan vendor:publish --tag=timeline-migrations
php artisan migrate
bash
php artisan make:timeline-event OrderShipped
blade
<livewire:timeline :causer="$team" :per-page="10" :filterable="false" />
<livewire:timeline :causer="$user" :only="['login', 'order_shipped']" />
bash
php artisan vendor:publish --tag=timeline-assets
bash
php artisan vendor:publish --tag=timeline-views
bash
php artisan vendor:publish --tag=timeline-translations
bash
php artisan make:timeline-event OrderShipped          # app/Timeline/OrderShipped.php
php artisan make:timeline-event Billing/InvoicePaid   # app/Timeline/Billing/InvoicePaid.php
php artisan make:timeline-event OrderShipped --force  # overwrite an existing class
bash
php artisan vendor:publish --tag=timeline-stubs
bash
php artisan timeline:prune --days=365
php artisan timeline:prune --days=90 --type=login --type=logout  # only these keys
bash
php artisan vendor:publish --tag=timeline-config