michaeljmeadows / created-by
A simple trait to add User relationships to Eloquent models to track creation, updating, and deletion.
Installs: 29
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/michaeljmeadows/created-by
Requires
- php: ^8.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
README
A simple trait to add User relationships to Eloquent models to track creation, updating, and deletion.
Installation
You can install the package via composer:
composer require michaeljmeadows/created-by
Usage
Assuming you already have a users table, add the following fields to the model migration:
$table->foreignId('created_by')->nullable()->constrained('users'); $table->foreignId('updated_by')->nullable()->constrained('users'); $table->foreignId('deleted_by')->nullable()->constrained('users');
If you're using UUID or ULID ID fields, replace foreignId in the above with foreignUuid or foreignUlid fields accordingly
Once the fields are added to the model, you can simply include the trait in your model's definition:
<?php namespace App\Models; use michaeljmeadows\Traits\CreatedBy; use Illuminate\Database\Eloquent\Model; class NewModel extends Model { use CreatedBy;
BelongsTo relationships to the User will now be accessible on your model:
$creator = $newModel->createdBy; $updater = $newModel->updatedBy; $deleter = $newModel->deletedBy;