An Esensi package, coded by Emerson Media.
Want to work with us on great Laravel applications? Email us at [email protected]
The Esensi/Model
package is just one package that makes up Esensi, a platform built on Laravel. This package uses PHP traits to extend Laravel's default Eloquent models and traits. Using traits allows for a high-degree of code reusability and extensibility. While this package provides some reasonable base models, developers are free to mix and match traits in any combination needed, being confident that the code complies to a reliable interface and is properly unit tested. For more details on the inner workings of the traits please consult the generously documented source code.
Have a project in mind? Email us at [email protected], or call 1.877.439.6665.
Notice: This code is specifically designed to be compatible with the Laravel Framework and may not be compatible as a stand-alone dependency or as part of another framework.
The simplest way to demonstrate the traits is to extend the base Esensi\Model\Model
. For example, if the application requires a simple blog, then the developer could create a Post
model that automatically handles validation, purging, hashing, encrypting, and even simplified relationship bindings by simply extending this ready-to-go model:
<?php
use \Esensi\Model\Model;
class Post extends Model {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'posts';
}
Pro Tip: Take a look at the generously commented
Esensi\Model\Model
source code for details on how to use individual traits with and without extending the default model.
If the application requires that the articles be sent to the trash before permanently deleting them, then the developer can just swap out the Esensi\Model\Model
with the soft deleting version Esensi\Model\SoftModel
like so:
<?php
use \Esensi\Model\SoftModel;
class Post extends SoftModel {
}
Pro Tip: While Laravel includes
SoftDeletingTrait
, Esensi expands upon this by also forcing the trait to comply with aSoftDeletingModelInterface
contract. This ensures a higher level of compatibility and code integrity.
Help Write Better Documentation: The documentation is still a work in progress. You can help others learn to reuse code by contributing better documentation as a pull request.
- Installation
- Validating Model Trait
- Auto-Validating on Save
- Manually Validating Model Attributes
- Handling Validation Errors
- Using Force Save
- Purging Model Trait
- Hashing Model Trait
- Encrypting Model Trait
- Auto-Encrypting on Set
- Auto-Decrypting on Get
- Manually Encrypting Model Attributes
- Checking Encryption State
- Soft Deleting Model Trait
- Using Soft Deletes
- Adding Custom Dates
- Using Force Delete
- Relating Model Trait
- Unit Testing
- Contributing
- Licensing
Add the esensi/model
package as a dependency to the application. Using Composer, this can be done from the command line:
composer require esensi/model 0.3.*
Or manually it can be added to the composer.json
file:
{
"require": {
"esensi/model": "0.3.*"
}
}
If manually adding the package, then be sure to run composer update
to update the dependencies.
This package includes the ValidatingModelTrait
which implements the ValidatingModelInterface
on any Eloquent
model that uses it. The ValidatingModelTrait
adds methods to Eloquent
models for:
- Automatic validation of models on
create()
,update()
,save()
,delete()
, andrestore()
method calls - Integration with Laravel's
Validation
facade to validate model attributes according to sets of rules - Integration with Laravel's
MessageBag
so that models can return errors when validation fails - Option to throw
ValidationException
when validation fails - Ability to
forceSave()
and bypass validation rules that would other wise prevent a model from saving - Automatic injection (or not) of the model's identifier for
unique
validation rules
Like all the traits, it is self-contained and can be used individually. Special credit goes to the very talented Dwight Watson and his Watson/Validating Laravel package which is the basis for this trait. Emerson Media collaborated with him as he created the package. Esensi wraps his traits with consistent naming conventions for the other Esensi model traits. Please review his package in detail to see the inner workings.
While developers can of course use the Model
or SoftModel
classes which already include the ValidatingModelTrait
, the following code will demonstrate adding auto-validation to any Eloquent
based model.
<?php
use \Esensi\Model\Contracts\ValidatingModelInterface;
use \Esensi\Model\Traits\ValidatingModelTrait;
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Post extends Eloquent implements ValidatingModelInterface {
use ValidatingModelTrait;
/**
* This tells whether or not the model should inject its identifier
* into the unique validation rules before attempting validation.
*
* @var boolean
*/
protected $injectUniqueIdentifier = true;
/**
* These are the default rules that the model will validate against.
* Developers will probably want to specify generic validation rules
* that would apply in any save operation vs. form or route
* specific validation rules. For simple models, these rules can
* apply to all save operations.
*
* @var array
*/
protected $rules = [
'title' => [ 'max:64' ],
'slug' => [ 'max:16', 'alpha_dash', 'unique' ],
'published' => [ 'boolean' ],
// ... more attribute rules
];
/**
* These are the rulesets that the model will validate against
* during specific save operations. Rulesets should be keyed
* by either the in progress event name of the save operation
* or a custom unique key for custom validation.
*
* The following rulesets are automatically applied during
* corresponding save operations:
*
* "creating" after "saving" but before save() is called (on new models)
* "updating" after "saving" but before save() is called (on existing models)
* "saving" before save() is called (and only if no "creating" or "updating")
* "deleting" when calling delete() method
* "restoring" when calling restore() method (on a soft-deleting model)
*
* @var array
*/
protected $rulesets = [
'creating' => [
'title' => [ 'required', 'max:64' ],
'slug' => [ 'required', 'alpha_dash', 'max:16', 'unique' ],
'published' => [ 'boolean' ],
// ... more attribute rules to validate against when creating
],
'updating' => [
'title' => [ 'required', 'max:64' ],
'slug' => [ 'required', 'alpha_dash', 'max:16', 'unique' ],
'published' => [ 'boolean' ],
// ... more attribute rules to validate against when updating
],
];
}
Then from the controller or repository the developer can interact with the Post
model's attributes, call the save()
method and let the Post
model handle validation automatically. For demonstrative purposes the following code shows this pattern from a simple route closure:
Route::post( 'posts', function()
{
// Hydrate the model from the Input
$attributes = Input::only( 'title', 'slug', 'published' );
$post = new Post( $attributes );
// Attempt to save, will return false on invalid model.
// Because this is a new model, the "creating" ruleset will
// be used to validate against. If it does not exist then the
// "saving" ruleset will be attempted. If that does not exist, then
// finally it will default to the Post::$rules.
if ( ! $post->save() )
{
// Redirect back to the form with the message bag of errors
return Redirect::to( 'posts' )
->withErrors( $post->getErrors() )
->withInput();
}
// Redirect to the new post
return Redirect::to( 'posts/' . $post->id );
});
Calling the save()
method on the newly created Post
model would instead use the "updating" ruleset from Post::$ruleset
while saving. If that ruleset did not exist then it would default to using the Post::$rules
.
This package includes the PurgingModelTrait
which implements the PurgingModelInterface
on any Eloquent
model that uses it. The PurgingModelTrait
adds methods to Eloquent
models for automatically purging attributes from the model just before write operations to the database. The trait automatically purges:
- attributes in the
$purgeable
property - attributes prefixed with an underscore (i.e.:
_private
) - attributes ending in
_confirmation
(i.e.:password_confirmation
)
Like all the traits, it is self-contained and can be used individually.
Pro Tip: This trait uses the
PurgingModelObserver
to listen for theeloquent.creating
andeloquent.updating
events before automatically purging the purgeable attributes. The order in which the traits are used in theModel
determines the event priority: if using theValidatingModelTrait
be sure to use it first so that the purging event listner is fired after the validating event listener has fired.
While developers can of course use the Model
or SoftModel
classes which already include the PurgingModelTrait
, the following code will demonstrate using automatic purging on any Eloquent
based model.
<?php
use \Esensi\Model\Contracts\PurgingModelInterface;
use \Esensi\Model\Traits\PurgingModelTrait;
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Post extends Eloquent implements PurgingModelInterface {
use PurgingModelTrait;
/**
* These are the attributes to purge before saving.
*
* Remember, anything prefixed with "_" or ending
* in "_confirmation" will automatically be purged
* and does not need to be listed here.
*
* @var array
*/
protected $purgeable = [
'analytics_id',
'_private_attribute',
'password_confirmation',
];
}
Pro Tip: From an efficiency stand point, it is theoretically better to assign all purgeable attributes in the
$purgeable
property including underscore prefixed and_confirmation
suffixed attributes since the$purgeable
property is checked first and does not require string parsing and comparisons.
The developer can now pass form input to the Post
model from a controller or repository and the trait will automatically purge the non-attributes before saving. This gets around those pesky "Unknown column" MySQL errors. For demonstrative purposes the following code shows this in practice from a simple route closure:
Route::post( 'posts', function( $id )
{
// Hydrate the model from the Input
$input = Input::all();
$post = new Post($input);
// At this point $post->analytics_id might exist.
// If we tried to save it, MySQL would throw an error.
// Save the Post
$post->save();
// At this point $post->analytics_id is for sure purged.
// It was filtered becaused it existed in Post::$purgeable.
});
It is also possible to manually purge attributes. The PurgingModelTrait
includes several helper functions to make manual manipulation of the $purgeable
property easier.
// Hydrate the model from the Input
$post = Post::find($id);
$post->fill( Input::all() );
// Manually purge attributes prior to save()
$post->purgeAttributes();
// Manually get the attributes
$post->getHashable(); // ['foo']
// Manually set the purgeable attributes
$post->setPurgeable( ['foo', 'bar'] ); // ['foo', 'bar']
// Manually add an attribute to the purgeable attributes
$post->addPurgeable( 'baz' ); // ['foo', 'bar', 'baz']
$post->mergePurgeable( ['zip'] ); // ['foo', 'bar', 'baz', 'zip']
$post->removePurgeable( 'foo' ); // ['bar', 'baz', 'zip']
// Check if an attribute is in the Post::$purgeable property
if ( $post->isPurgeable( 'foo' ) )
{
// ... foo is not purgeable so this would not get executed
}
// Do not run purging for this save only.
// This is useful when purging is enabled
// but needs to be temporarily bypassed.
$post->saveWithoutPurging();
// Disable purging
$post->setPurging(false); // a value of true would enable it
// Run purging for this save only.
// This is useful when purging is disabled
// but needs to be temporarily ran while saving.
$post->saveWithPurging();
This package includes the HashingModelTrait
which implements the HashingModelInterface
on any Eloquent
model that uses it. The HashingModelTrait
adds methods to Eloquent
models for automatically hashing attributes on the model just before write operations to the database. The trait includes the ability to:
- automatically hash attributes in the
$hashable
property - manually hash a value using the
hash()
method - compare a plain text value with a hash using the
checkHash()
method - check if a value is hashed using the
isHashed()
method - swap out the
HasherInterface
used using thesetHasher()
method
Like all the traits, it is self-contained and can be used individually.
Pro Tip: This trait uses the
HashingModelObserver
to listen for theeloquent.creating
andeloquent.updating
events before automatically hashing the hashable attributes. The order in which the traits are used in theModel
determines the event priority: if using theValidatingModelTrait
be sure to use it first so that the hashing event listner is fired after the validating event listener has fired.
While developers can of course use the Model
or SoftModel
classes which already include the HashingModelTrait
, the following code will demonstrate using automatic hashing on any Eloquent
based model. For this example, the implementation of automatic hashing will be applied to a User
model which requires the password to be hashed on save:
<?php
use \Esensi\Model\Contracts\HashingModelInterface;
use \Esensi\Model\Traits\HashingModelTrait;
use \Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent implements HashingModelInterface {
use HashingModelTrait;
/**
* These are the attributes to hash before saving.
*
* @var array
*/
protected $hashable = [ 'password' ];
}
Pro Tip: The
HashingModelTrait
is a great combination for thePurgingModelTrait
. Often hashable attributes need to be confirmed and using thePurgingModelTrait
, the model can be automatically purged of the annoying_confirmation
attributes before writing to the database. While theuse
order of these two traits is not important relative to each other, it is important touse
them afterValidatingModelTrait
if that trait is used as well. Otherwise, the model will purge or hash the attributes before validating.
The developer can now pass form input to the User
model from a controller or repository and the trait will automatically hash the password
before saving. For demonstrative purposes the following code shows this in practice from a simple route closure:
Route::post( 'account', function( $id )
{
// Hydrate the model from the Input
$user = Auth::user();
$user->password = Input::get('password');
// At this point $user->password is still plain text.
// This allows for the value to be checked by validation.
// Save the User
$user->save();
// At this point $user->password is for sure hashed.
// It was hashed becaused it existed in User::$hashable.
});
It is also possible to manually hash attributes. The HashingModelTrait
includes several helper functions to make manual manipulation of the $hashable
property easier.
// Hydrate the model from the Input
$post = User::find($id);
$post->password = Input::get('password');
// Manually hash attributes prior to save()
$post->hashAttributes();
// Manually get the attributes
$post->getHashable(); // ['foo']
// Manually set the hashable attributes
$post->setHashable( ['foo', 'bar'] ); // ['foo', 'bar']
// Manually add an attribute to the hashable attributes
$post->addHashable( 'baz' ); // ['foo', 'bar', 'baz']
$post->mergeHashable( ['zip'] ); // ['foo', 'bar', 'baz', 'zip']
$post->removeHashable( 'foo' ); // ['bar', 'baz', 'zip']
// Check if an attribute is in the User::$hashable property
if ( $post->isHashable( 'foo' ) )
{
// ... foo is not hashable so this would not get executed
}
// Check if an attribute is already hashed
if ( $post->isHashed( 'foo' ) )
{
// ... if foo were hashed this would get executed
}
// Check if the password when hashed matches the stored password.
// This is just a unified shorthand to Crypt::checkHash().
if ( $post->checkHash( 'password123', $post->password ) )
{
// ... if the password matches you could authenticate the user
}
// Swap out the HasherInterface used
$post->setHasher( new MyHasher() );
// Do not run hashing for this save only.
// This is useful when hashing is enabled
// but needs to be temporarily bypassed.
$post->saveWithoutHashing();
// Disable hashing
$post->setHashing(false); // a value of true would enable it
// Run hashing for this save only.
// This is useful when hashing is disabled
// but needs to be temporarily ran while saving.
$post->saveWithHashing();
This package includes the EncryptingModelTrait
which implements the EncryptingModelInterface
on any Eloquent
model that uses it. The EncryptingModelTrait
adds methods to Eloquent
models for automatically encrypting attributes on the model whenever they are set and for automatically decrypting attributes on the model whenever they are got. The trait includes the ability to:
- automatically encrypt attributes in the
$encryptable
property when setting them - automatically decrypt attributes in the
$encryptable
property when getting them - manually encrypt/decrypt a value using the
encrypt()
anddecrypt()
methods - check if a value is encrypted using the
isEncrypted()
method - swap out the encrypter class used using the
setEncrypter()
method
Like all the traits, it is self-contained and can be used individually. Be aware, however, that using this trait does overload the magic __get()
and __set()
methods of the model (see Esensi\Model\Model source code for how to deal with overloading conflicts).
It is also possible to manually encrypt attributes. The EncryptingModelTrait
includes several helper functions to make manual manipulation of the $encryptable
property easier.
// Hydrate the model from the Input
$post = Model::find($id);
$post->secret = Input::get('secret'); // automatically encrypted
// Manually encrypt attributes prior to save()
$post->encryptAttributes();
// Manually encrypt and decrypte a value
$encrypted = $post->encrypt( 'plain text' );
$decrypted = $post->decrypt( $encrypted ); // plain text
// Manually get the attributes
$post->getEncryptable(); // ['foo']
// Manually set the encryptable attributes
$post->setEncryptable( ['foo', 'bar'] ); // ['foo', 'bar']
// Manually add an attribute to the encryptable attributes
$post->addEncryptable( 'baz' ); // ['foo', 'bar', 'baz']
$post->mergeEncryptable( ['zip'] ); // ['foo', 'bar', 'baz', 'zip']
$post->removeEncryptable( 'foo' ); // ['bar', 'baz', 'zip']
// Check if an attribute is in the Model::$encryptable property
if ( $post->isEncryptable( 'foo' ) )
{
// ... foo is not encryptable so this would not get executed
}
// Check if an attribute is already encrypted.
// You could also check $post->isDecrypted( 'foo' ).
if ( $post->isEncrypted( 'foo' ) )
{
// ... if foo were encrypted this would get executed
}
// Swap out the encrypter class used
$post->setEncrypter( new MyEncrypter() );
// Disable encrypting
$post->setEncrypting(false); // a value of true would enable it
This package includes the SoftDeletingModelTrait
which implements the SoftDeletingModelInterface
on any Eloquent
model that uses it. The SoftDeletingModelTrait
wraps the default Eloquent
model's SoftDeletingTrait
for a unified naming convention and stronger interface hinting. The trait also includes the ability to set additional dates in the $dates
property without having to remember to add deleted_at
.
Like all the traits, it is self-contained and can be used individually. As a convenience, the Esensi\Model\SoftModel
extends the Esensi\Model\Model
and implements the trait already. The developer can just extend the SoftModel
and not have to refer to the Laravel soft deleting documentation again.
Pro Tip: Just because a model uses the
SoftDeletingModelTrait
does not mean that the database has thedeleted_at
column in its table. Be sure to add$table->softDeletes();
to a table migration.
This package includes the RelatingModelTrait
which implements the RelatingModelInterface
on any Eloquent
model that uses it. The RelatingModelTrait
adds methods to Eloquent
models for automatically resolving related models:
- from simplified configs using the
$relationships
property - as magic method calls such as
Post::find($id)->comments()->all()
- as magic attribute calls such as
Post::find($id)->author
Like all the traits, it is self-contained and can be used individually. Be aware, however, that using this trait does overload the magic __call()
and __get()
methods of the model (see Esensi\Model\Model source code for how to deal with overloading conflicts). Special credit goes to Phillip Brown and his Philipbrown/Magniloquent Laravel package which inspired this trait.
While developers can of course use the Model
or SoftModel
classes which already include the RelatingModelTrait
, the following code will demonstrate adding simplified relationship bindings to any Eloquent
based model.
<?php
use \Esensi\Model\Contracts\RelatingModelInterface;
use \Esensi\Model\Traits\RelatingModelTrait;
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Post extends Eloquent implements RelatingModelInterface {
use RelatingModelTrait;
/**
* These are the relationships that the model should set up.
* Using PHP and Laravel's magic, these relationship keys
* resolve to the actual models automatically.
*
* @example relationship bindings:
*
* [ 'hasOne', 'related', 'foreignKey', 'localKey' ]
* [ 'hasMany', 'related', 'foreignKey', 'localKey' ]
* [ 'hasManyThrough', 'related', 'through', 'firstKey', 'secondKey' ]
* [ 'belongsTo', 'related', 'foreignKey', 'otherKey', 'relation' ]
* [ 'belongsToMany', 'related', 'table', 'foreignKey', 'otherKey', 'relation' ]
* [ 'morphOne', 'related', 'name', 'type', 'id', 'localKey' ]
* [ 'morphMany', 'related', 'name', 'type', 'id', 'localKey' ]
* [ 'morphTo', 'name', 'type', 'id' ]
* [ 'morphToMany', 'related', 'name', 'table', 'foreignKey', 'otherKey', 'inverse' ]
* [ 'morphByMany', 'related', 'name', 'table', 'foreignKey', 'otherKey' ]
*
* @var array
*/
protected $relationships = [
// Bind Comment model as a hasMany relationship.
// Use Post::comments() to query the relationship.
'comments' => [ 'hasMany', 'Comment' ],
// Bind User model as a belongsTo relationship.
// Use $post->author to get the User model.
'author' => [ 'belongsTo', 'User' ]
];
}
The developer can now use the Post
model's relationships from a controller or repository and the trait will automatically resolve the relationship bindings. For demonstrative purposes the following code shows this pattern from a simple route closure:
Route::get( 'posts/{id}/comments', function( $id )
{
// Retrieve the post by ID
$post = Post::find( $id );
// Query the post for all the related comments.
// The trait will resolve the "comments" from
// the Post::$relationships bindings.
$comments = $post->comments()->all();
// It is also possible to shorten this using the
// magic attributes instead. It is equivalent to
// the above call.
$comments = $post->comments;
// ... pass the $comments collection to the view
});
The Esensi platform includes other great packages just like this Esensi/Model package. This package is currently tagged as 0.3.x
because the other platform packages are not ready for public release. While the others may still be under development, this package already includes features that would be mature enough for a 1.x
release including unit testing and extensive testing in real-world applications.
This package uses PHPUnit to automate the code testing process. It is included as one of the development dependencies in the composer.json
file:
{
"require-dev": {
"phpunit/phpunit": "4.1.*",
"mockery/mockery": "0.9.*"
}
}
The test suite can be ran from the command line using the phpunit
test runner:
phpunit ./tests
Pro Tip: Please help the open-source community out by including good code test coverage with your pull requests. The Esensi development team will review pull requests with unit tests and passing tests as a priority. Significant code changes that do not include unit tests will not be merged.
Emerson Media is proud to work with some of the most talented developers in the PHP community. The developer team welcomes requests, suggestions, issues, and of course pull requests. When submitting issues please be as detailed as possible and provide code examples where possible. When submitting pull requests please follow the same code formatting and style guides that the Esensi code base uses. Please help the open-source community out by including good code test coverage with your pull requests. All pull requests must be submitted to the version branch to which the code changes apply.
Note: The development team does its best to address all issues on Wednesdays. Pull requests are reviewed in priority followed by urgent bug fixes. Each week the package dependencies are re-evaluated and updates are made for new tag releases.
Copyright (c) 2014 Emerson Media, LP
This package is released under the MIT license. Please see the LICENSE.txt file distributed with every copy of the code for commercial licensing terms.