PHP code example of layman / laravel-logger

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

    

layman / laravel-logger example snippets


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Layman\LaravelLogger\Facades\Logger;

// 数据库日志使用方式
Event::listen('eloquent.created: *', function ($event, array $data) {
    $model = $data[0];
    // 建议排除日志写入操作
    if ($model instanceof \Layman\LaravelLogger\Models\Logger) {
        return;
    }
    $type = Str::between($event, 'eloquent.', ':');
    // 必须使用withoutEvents 在回调函数执行期间,临时禁用 Eloquent 模型事件(created、updated、deleted 等),防止事件被触发。
    Model::withoutEvents(function () use ($model, $type) {
        Logger::setUserid(11)
            ->setType($type)
            ->setModel(get_class($model))
            ->setOld([])
            ->setNew($model->getAttributes())
            ->create();
    });
});
bash
php artisan vendor:publish --provider="Layman\LaravelLogger\LoggerServiceProvider" --tag=logger

// SESSION_DRIVER=database 执行
php artisan make:session-table

php artisan migrate

php artisan db:seed --class="Layman\\LaravelLogger\\Database\\Seeders\\LoggerUserSeeder"