Aller au contenu
← Retour aux projets

Laravel Created By

Laravel Created By

#Laravel Created By

Buy Me A Coffee

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This Laravel package automatically logs the currently logged-in user's ID to the created_by, updated_by, deleted_by, and restored_by fields of your Eloquent models. It also automatically timestamps the restored_at field when a model is restored. This simplifies the tracking of data modifications and provides valuable auditing capabilities. The package is easy to install and configure, seamlessly integrating with your existing Laravel application.

#Installation

You can install the package via composer:

composer require jeffersongoncalves/laravel-created-by

#Usage

Add in columns our table.

Schema::create('posts', function (Blueprint $table) {
    $table->createdBy();
    $table->updatedBy();
    $table->deletedBy();
    $table->restoredBy();
    $table->restoredAt();
    $table->softDeletes();
});

Your model add traits:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use JeffersonGoncalves\CreatedBy\Models\Concerns\WithCreatedBy;
use JeffersonGoncalves\CreatedBy\Models\Concerns\WithUpdatedBy;
use JeffersonGoncalves\CreatedBy\Models\Concerns\WithDeletedBy;
use JeffersonGoncalves\CreatedBy\Models\Concerns\WithRestoredBy;
use JeffersonGoncalves\CreatedBy\Models\Concerns\WithRestoredAt;

class Post extends Model
{
    use SoftDeletes;
    use WithCreatedBy;
    use WithUpdatedBy;
    use WithDeletedBy;
    use WithRestoredBy;
    use WithRestoredAt;
}

#Configuration

You can publish the config file with:

php artisan vendor:publish --tag="created-by-config"

This is the contents of the published config file:

return [
    // The authentication guard used to resolve the current user (null = default guard).
    'guard' => null,

    // The column names used to store the audit information.
    'columns' => [
        'created_by' => 'created_by',
        'updated_by' => 'updated_by',
        'deleted_by' => 'deleted_by',
        'restored_by' => 'restored_by',
        'restored_at' => 'restored_at',
    ],
];

#Query Macros

Each trait registers query builder macros so you can filter and eager load by the audit columns:

// Filter by the user id stored in the column
Post::query()->createdBy($userId)->get();
Post::query()->updatedBy($userId)->get();
Post::query()->deletedBy($userId)->get();
Post::query()->restoredBy($userId)->get();

// Eager load the related user relationship
Post::query()->withCreatedBy()->get();
Post::query()->withUpdatedBy()->get();
Post::query()->withDeletedBy()->get();
Post::query()->withRestoredBy()->get();

#Testing

composer test

#Changelog

Please see CHANGELOG for more information on what has changed recently.

#Contributing

Please see CONTRIBUTING for details.

#Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

#Credits

#License

The MIT License (MIT). Please see License File for more information.

Nouvelle version disponible.