PHP code example of devdabour / laravel-loggable

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

    

devdabour / laravel-loggable example snippets


use Devdabour\LaravelLoggable\Traits\Loggable;

class Product extends Model
{
    use Loggable;

    protected static array $logAttributes = [
        'name' => 'Product Name',
        'price' => 'Price',
        'status' => 'Status',
    ];
}

class Product extends Model
{
    use Loggable;

    // Enable metadata logging
    protected static $logMetadata = true;

    // Add custom metadata
    protected static $logAdditionalData = [
        'custom_field' => 'Custom Label',
        'department' => 'Department Name'
    ];
}

class Product extends Model
{
    use Loggable;

    protected static $logRelationships = [
        'category' => ['id', 'name'],
        'tags' => ['id', 'name'],
        'manufacturer' => ['id', 'company_name']
    ];
}

use Devdabour\LaravelLoggable\Traits\Loggable;
use Spatie\Translatable\HasTranslations;

class Phase extends Model
{
    use HasFactory;
    use Loggable;
    use HasTranslations;

    public $translatable = ['name'];

    // Define language-specific labels in a nested structure
    protected static array $logAttributes = [
        'name' => [
            'en' => 'English Name',
            'ar' => 'Arabic Name',
        ],
    ];
}

protected static array $jsonFields = ['name', 'description'];

protected static array $logAttributes = [
    'name_en' => 'English Name',
    'name_ar' => 'Arabic Name',
];

use Devdabour\LaravelLoggable\Traits\Loggable;
use Spatie\Translatable\HasTranslations;

class Phase extends Model
{
    use HasFactory;
    use Loggable;
    use HasTranslations;

    public $translatable = ['name'];
    
    // That's it! All languages will be logged automatically
    // Each language will appear as "name (en)", "name (ar)", etc.
}

class UserSettings extends Model
{
    use Loggable;

    protected $casts = [
        'preferences' => 'json',
        'notifications' => 'json',
    ];

    // Tell Loggable which fields contain JSON data
    protected static array $jsonFields = ['preferences', 'notifications'];

    // Optional: Map specific JSON paths to readable labels
    protected static array $logAttributes = [
        'preferences' => [
            'theme' => 'UI Theme',
            'language' => 'Display Language',
        ],
        'notifications.email' => 'Email Notifications',
        'notifications.push' => 'Push Notifications',
    ];
}

// Database JSON: {"settings":{"theme":"dark","notifications":{"email":true,"push":false}}}

// Activity log will show:
// settings.theme: dark
// settings.notifications.email: true
// settings.notifications.push: false

protected static array $logAttributes = [
    'settings.theme' => 'Theme Setting',
    'settings.notifications.email' => 'Email Notifications',
];

// Check if a string is valid JSON
$model->isJson($string);

// Get human-readable attributes
$attributes = $model->getLogAttributes();

// Get custom log name
$logName = $model->getLogName();
bash
php artisan vendor:publish --provider="Devdabour\LaravelLoggable\Providers\LoggableServiceProvider"
bash
php artisan migrate