Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcdonald-uk committed Mar 10, 2016
0 parents commit 6e5b425
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Wildside/Userstamps

Provides an Eloquent trait to automatically maintain created_by and updated_by (and deleted_by when using SoftDeletes) on your models.

## Requirements

* This package requires PHP 5.6+
* It has been tested against Laravel 5.2 (though should work with previous versions of Laravel 5).

## Installation

Require this package in your `composer.json` and update composer.

```php
"wildside/userstamps": "0.1.0"
```

Migrate your Model's table to include a `created_by` and `updated_by` (and `deleted_by` if using `SoftDeletes`).

```php
$table -> unsignedInteger('created_by') -> after('created_at');
$table -> unsignedInteger('updated_by') -> after('updated_at');
```

Load the trait in your Model.

```php
use Wildside\Userstamps\Userstamps;

class Example extends Model {

use Userstamps;
}
```
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "wildside/userstamps",
"description": "Eloquent trait to automatically maintain created_by and updated_by (and deleted_by when using SoftDeletes) on your models",
"license": "MIT",
"keywords": [
"laravel",
"eloquent",
"userstamps",
"created_by",
"updated_by",
"deleted_by"
],
"authors": [
{
"name": "WildSide",
"email": "hello@wildside.uk",
"homepage": "https://wildside.uk"
}
],
"require": {
"php": ">=5.6"
},
"autoload": {
"psr-4": {
"Wildside\\Userstamps\\": "src"
}
}
}
19 changes: 19 additions & 0 deletions src/Listeners/Creating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Wildside\Userstamps\Listeners;

use Illuminate\Auth\Guard;

class Creating {

/**
* When the model is being created.
*
* @param Illuminate\Database\Eloquent $model
* @return void
*/
public function handle($model)
{
$model -> created_by = auth() -> id();
}
}
22 changes: 22 additions & 0 deletions src/Listeners/Deleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Wildside\Userstamps\Listeners;

use Illuminate\Auth\Guard;

class Deleting {

/**
* When the model is being deleted.
*
* @param Illuminate\Database\Eloquent $model
* @return void
*/
public function handle($model)
{
$model -> deleted_by = auth() -> id();

$model -> flushEventListeners();
$model -> save();
}
}
19 changes: 19 additions & 0 deletions src/Listeners/Restoring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Wildside\Userstamps\Listeners;

use Illuminate\Auth\Guard;

class Restoring {

/**
* When the model is being restored.
*
* @param Illuminate\Database\Eloquent $model
* @return void
*/
public function handle($model)
{
$model -> deleted_by = null;
}
}
19 changes: 19 additions & 0 deletions src/Listeners/Updating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Wildside\Userstamps\Listeners;

use Illuminate\Auth\Guard;

class Updating {

/**
* When the model is being updated.
*
* @param Illuminate\Database\Eloquent $model
* @return void
*/
public function handle($model)
{
$model -> updated_by = auth() -> id();
}
}
37 changes: 37 additions & 0 deletions src/Userstamps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Wildside\Userstamps;

trait Userstamps {

/**
* Boot the userstamps trait for a model.
*
* @return void
*/
public static function bootUserstamps()
{
static::registerListeners();
}

/**
* Register events we need to listen for.
*
* @return void
*/
public static function registerListeners()
{
static::creating('Wildside\Userstamps\Listeners\Creating@handle');
static::updating('Wildside\Userstamps\Listeners\Updating@handle');

if( method_exists(get_called_class(), 'deleting') )
{
static::deleting('Wildside\Userstamps\Listeners\Deleting@handle');
}

if( method_exists(get_called_class(), 'restoring') )
{
static::restoring('Wildside\Userstamps\Listeners\Restoring@handle');
}
}
}

0 comments on commit 6e5b425

Please sign in to comment.