Code Monkey home page Code Monkey logo

model-log's Introduction

Laravel ModelLog

Latest Stable Version Total Downloads License

A Laravel package to automatically log attributes changes on any of your app models.

About package

This package is intended for tracking changes of your Eloquent models inside your Laravel application. A new DB table will be created and everytime a model attribute is updated an entry will be automatically created on the DB log table.

Installation

Install package using Composer.

$ composer require tpenaranda/model-log

Run migrations to create ModelLog table.

$ php artisan migrate

[This step is not needed on Laravel >= 5.5] Add service provider and create ModelLog DB table.

Add service provider and alias in config/app.php

    'providers' => [
        ...
        TPenaranda\ModelLog\Providers\ModelLogServiceProvider::class,
    ],
    'aliases' => [
        ...
        'ModelLogEntry' => TPenaranda\ModelLog\ModelLogEntry::class,
        'ObservedByModelLog' => TPenaranda\ModelLog\Traits\ObservedByModelLog::class,
    ],

Run ModelLog command in order to create ModelLog DB table.

$ php artisan model-log:create-log-table

Usage

Add 'ObservedByModelLog' trait to your model and specify attributes you want to observe/track for changes.

use TPenaranda\ModelLog\Traits\ObservedByModelLog;

class MyModel extends Model
{
    use ObservedByModelLog;

    protected $log = ['my_attribute', 'track_this_column_too'];
}

Now after every update on that model, observed attributes will be logged automatically. Use protected $log = 'all'; (notice the string, not array) to log any change.

Retrieve log entries:

$my_model->logEntries;

Advanced usage

Retrieve log entries using query scopes:

\ModelLogEntry::whereModelClass('App\MyModel')->whereAttribute('my_attribute')->whereTo('value_after_change')->get();

Available scopes:

  • whereModel(<object>): Get logs of an specific Eloquent Model (example: get log data of MyModel ID #4).
  • whereModelClass(<string/object>): Get logs for an specific model class (example: get entries where MyModel class is involved, regardless of any IDs).
  • whereAttribute(<string>): Get only logs where some specific attribute was changed.
  • whereFrom(<string>): Get only logs with an specific initial value.
  • whereTo(<string>): Get only logs with an specific end value.
  • ModifiedByUser(<numeric/object>): Get changes done by some specific user. Allowed parameters: null, numeric IDs or User object.

The following scopes only accept Carbon objects as parameters:

  • loggedBefore(<Carbon object>): Retrieve only entries logged prior to specific date.
  • loggedAfter(<Carbon object>): Retrieve only entries logged after specific date.
  • withinDateRange(<Carbon object>, <Carbon object>): Retrieve only entries logged after first parameter and prior to second parameter.

Create (or drop and create) ModelLog table manually:

$ php artisan model-log:create-log-table

Flush ModelLog table:

\ModelLogEntry::flushAll();

model-log's People

Contributors

tpenaranda avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

xianlinzhang

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.