Giter VIP home page Giter VIP logo

userstamps's Introduction

Laravel Eloquent Userstamps

GitHub issues GitHub forks GitHub stars

A simple package to load & insert userstamps for a model

Requirements

  • This package requires PHP 5.6+
  • It works with Laravel 5.4 or later (and may work with earlier versions too).

Original

This package was forked from this repository and I just modified some things that I need in my project. I had problems when changing my Auth::guard from web to api so I modified it to fit my needs.

Installation

Step 1: Add this to your composer.json.

{
     "require": {
     "benyslo/userstamps": "dev-fixes",
     },
     "repositories": [
          {
               "type": "vcs",
               "url": "https://github.com/BenySLO/userstamps"
          },
     ],
 }

Step 2: composer update

Usage

Load the trait in your model and see the magic.

Scenario 1 : Load Userstamps For A Model

You can configure this package to autoload the userstamp along with your model. This will be the case when userstamp is being set in controller or some where else manually.

use BenySLO\Userstamps\UserstampTrait;

class Post extends Model {

    use UserstampTrait;

    protected $userstamps = [
       'created_by',
       'updated_by',
       'submitted_by',
       'deleted_by'
    ];
}

And then you can autoload these userstamps like,

$posts = Post::withUserstamps()->get();

This will allow you to access the defined userstamps on your model as dynamic relationships

$post->createdByUser;
$post->updatedByUser;
$post->submittedByUser;
$post->deletedByUser;

Scenario 2 : Insert & Load Userstamps For A Model

You can configure this package to handle the userstamp insertion behind the scenes. This will also load those userstamps when you will fetch the records with eloquent. Auto insert will depend on,

  1. Event ('creating', 'saving', 'updating', 'deleting')
  2. Field
  3. Expression
use BenySLO\Userstamps\UserstampTrait;

class Post extends Model {

    use UserstampTrait;

    protected $userstamps = [
       // This userstamp should be set when 'creating' event is invoked.
       'created_by' => [
            'depends_on_event' => 'creating',
       ],
       // This userstamp should be set when 'creating' or 'updating' event is invoked.
       // This is an example, if a userstamp depends on multiple events
       'updated_by' => [
            'depends_on_event' => ['creating', 'updating'],
       ],
       'deleted_by' => [
             'depends_on_event' => 'deleting',
       ],

       // This userstamp should be set if "is_archived" is dirty (has some change in value)
       'archived_by' => [
            'depends_on_field' => 'is_archived'
       ],

       // This userstamp should be set if "updating" event is invoked on this model,
       // and "is_submitted" is dirty (has some change in value)
       'submitted_by'=> [
            'depends_on_event' => 'updating',
            'depends_on_field' => 'is_submitted'
       ],

       // This userstamp should be set if "updating" event is invoked on this model,
       // and provided expression evaluates to true
       'suspended_by' => [
          'depends_on_event' => 'updating',
          'depends_on_expression' => '$api_hits > 100' // $api_hits is a model field i.e $model->api_hits
       ],
       .............,
       ..............,
    ];
}

And then you can autoload these userstamps like,

$posts = Post::withUserstamps()->get();

This will allow you to access the defined userstamps on your model as dynamic relationships

$post->createdByUser;
$post->updatedByUser;
$post->archivedByUser;
$post->submittedByUser;
$post->suspendedByUser;

License

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

userstamps's People

Contributors

eavio avatar hmshafeeq 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.