Skip to content

Commit

Permalink
[ci skip] Updated README.md to document helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcdonald-uk committed Sep 20, 2019
1 parent 940d866 commit c2c7cbe
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

## About Laravel Userstamps

Laravel Userstamps provides an Eloquent trait which automatically handles `created_by` and `updated_by` columns on your model, populated by the currently authenticated user in your application.
Laravel Userstamps provides an Eloquent trait which automatically handles `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.

Expand Down Expand Up @@ -65,15 +65,15 @@ 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.

```php
$model -> creator; // the user who created the model
$model -> editor; // the user who last updated the model
Expand All @@ -87,13 +87,13 @@ $model -> stopUserstamping(); // stops userstamps being maintained on the model
$model -> startUserstamping(); // resumes userstamps being maintained on the model
```

## Limitations
## Workarounds

This package works by 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 would happen if mass updating or deleting models, or their relations.
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:

Expand All @@ -104,14 +104,34 @@ $model->foos->each(function ($item) {
});
```

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

```php
$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:

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

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

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

## Sponsors

<a href="https://wildside.uk">
Expand Down

0 comments on commit c2c7cbe

Please sign in to comment.