limanweb / eloquent-extensions
Extensions for Laravel Eloquent\Model
Installs: 4 442
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=7.2.0
This package is auto-updated.
Last update: 2024-04-17 17:00:17 UTC
README
Extensions for Laravel Eloquent\Model and other classes
- Trait HasUserstamps for filling userstamp fields
created_at
,updated_at
,deleted_at
by authorized user ID
Installation
Run:
composer require "limanweb/eloquent-extension"
Package contents
- /Models
- /Concerns
- HasUsertimestamps.php - trait for userstamps filling in model
- HasCompositePrimaryKey.php - trait for using in model composite primary key
- HasBuilderConfigurator - draft of trait to build queries with request params
- /Concerns
Usage
HasUserstamps
Add into create or update table migration fields for userstamps created_at
, updated_at
and deleted_at
.
For examle modify CreateUsersTable migration.
class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { ... // add userstamps fields $table->bigInteger('created_by')->nullable(); $table->bigInteger('updated_by')->nullable(); // if SoftDeletes trait will be used in model then add deleted_by field // $table->bigInteger('deleted_by')->nullable(); }); } ... }
In the model you must:
- declare using of trait Limanweb\EloquentExt\Models\Concerns\HasUserstamps
- use HasUserstamps trait in the model
- enable userstamps by define public property
$userstamps
withtrue
value
... use Limanweb\EloquentExt\Models\Concerns\HasUserstamps; // (1) declare class User extends Authenticatable { use Notifiable; use HasUserstamps; // (2) use trait in the model public $userstamps = true; // (3) enable userstamps ... }
The created_by
and updated_by
fields in your model will now be populated in the same way as the timestamp fields when you create and update the model. If your model uses SoftDeletes traite, will also be processed field, deleted_by
.
HasCompositePrimaryKey
In the model you must:
- declare using of trait Limanweb\EloquentExt\Models\Concerns\HasCompositePrimaryKey
- use HasCompositePrimaryKey trait in the model
- set $incrementing model property as
false
- set $primaryKey model property as array complex primary key part names
... use Limanweb\EloquentExt\Models\Concerns\HasCompositePrimaryKey; // (1) declare trait class Example extends Model { use HasCompositePrimaryKey; // (2) use trait in the model public $incrementing = false; // (3) protected $primaryKey = ['part1', 'part2']; // (4) ... }
Example of using
>>> $m = Example::find([65, 275]); => App\Example {#3837 part1: 65, part2: 275, message: "record 65:275", } >>> $m->getKey(); => [ 65, 275, ]