-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6e5b425
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |