Skip to content
Jarek Tkaczyk edited this page Jul 18, 2015 · 4 revisions

Eloquence base trait

It is an entry point for other traits and is required in each model that you'd like to use any functionality of the package in. It also provides some helper methods.

Helpers

Eloquence defines 2 helper methods that come in handy pretty often: hasColumn and getColumnListing

User::hasColumn('username') // bool
User::getColumnListing() // array

getColumnListing methods caches the result statically, so it runs at most one query during the request. Mind that it will return raw column names (without applying any mappings from the Mappable etc.).

Hooks

Eloquence hooks on the Model in order to apply enhancements to the core methods.

It lets you use addons as a stack (custom implementation - Pipeline similar to the one used in the Laravel core by middlewares). It evaluates the addons (traits) in either FIFO or LIFO manner, depending on the method, so the order of the traits matters.

For example getAttribute falls into line with FIFO rule:

class User extends Model {
  use Eloquence;
  use Mappable, Mutable;

  protected $maps = ['name' => 'profile.name'];
  protected $getterMutators = ['name' => 'ucwords'];
  protected $setterMutators = ['name' => 'strtolower'];
}

// then
$user->name
// calls:
// 1. Mappable - fetches $user->profile->name
// 2. Mutable - applies ucfirst on the fetched value

while setAttribute aheres to LIFO:

$user->name = 'Jarek Tkaczyk'
// calls:
// 1. Mutable mutates value
// 2. Mappable sets value on $user->profile
Clone this wiki locally