Giter Club home page Giter Club logo

laravel-userstamps's Introduction

Build Status Total Downloads Latest Stable Version License

About Laravel Userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains created_by and updated_by columns on your model, populated by the currently authenticated user in your application.

When using the Laravel SoftDeletes trait, a deleted_by column is also handled by this package.

Installing

This package requires Laravel 5.2 or later running on PHP 5.6 or higher.

This package can be installed using composer:

composer require wildside/userstamps

Usage

Your model will need to include a created_by and updated_by column, defaulting to null.

If using the Laravel SoftDeletes trait, it will also need a deleted_by column.

The column type should match the type of the ID column in your user's table. In Laravel <= 5.7 this defaults to unsignedInteger. For Laravel >= 5.8 this defaults to unsignedBigInteger.

An example migration:

$table->unsignedBigInteger('created_by')->nullable();
$table->unsignedBigInteger('updated_by')->nullable();

You can now load the trait within your model, and userstamps will automatically be maintained:

use Wildside\Userstamps\Userstamps;

class Foo extends Model {

    use Userstamps;
}

Optionally, should you wish to override the names of the created_by, updated_by or deleted_by columns, you can do so by setting the appropriate class constants on your model. Ensure you match these column names in your migration.

use Wildside\Userstamps\Userstamps;

class Foo extends Model {

    use Userstamps;

    const CREATED_BY = 'alt_created_by';
    const UPDATED_BY = 'alt_updated_by';
    const DELETED_BY = 'alt_deleted_by';
}

When using this trait, helper relationships are available to let you retrieve the user who created, updated and deleted (when using the Laravel SoftDeletes trait) your model.

$model->creator; // the user who created the model
$model->editor; // the user who last updated the model
$model->destroyer; // the user who deleted the model

Methods are also available to temporarily stop the automatic maintaining of userstamps on your models:

$model->stopUserstamping(); // stops userstamps being maintained on the model
$model->startUserstamping(); // resumes userstamps being maintained on the model

Workarounds

This package works by hooking into Eloquent's model event listeners, and is subject to the same limitations of all such listeners.

When you make changes to models that bypass Eloquent, the event listeners won't be fired and userstamps will not be updated.

Commonly this will happen if bulk updating or deleting models, or their relations.

In this example, model relations are updated via Eloquent and userstamps will be maintained:

$model->foos->each(function ($item) {
    $item->bar = 'x';
    $item->save();
});

However in this example, model relations are bulk updated and bypass Eloquent. Userstamps will not be maintained:

$model->foos()->update([
    'bar' => 'x',
]);

As a workaroud to this issue two helper methods are available - updateWithUserstamps and deleteWithUserstamps. Their behaviour is identical to update and delete, but they ensure the updated_by and deleted_by properties are maintained on the model.

You generally won't have to use these methods, unless making bulk updates that bypass Eloquent events.

In this example, models are bulk updated and userstamps will not be maintained:

$model->where('name', 'foo')->update([
    'name' => 'bar',
]);

However in this example, models are bulk updated using the helper method and userstamps will be maintained:

$model->where('name', 'foo')->updateWithUserstamps([
    'name' => 'bar',
]);

Sponsors

This open-source software is developed and maintained by WILDSIDE.

License

This open-source software is licensed under the MIT license.

laravel-userstamps's People

Contributors

mattmcdonald-uk avatar tkayfun avatar mazyvan avatar justinbyrne avatar mahmoudmohamedramadan avatar romeromsk avatar stylecibot avatar geisi avatar tacone avatar

Watchers

James Cloos avatar

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.