Installation:
composer require kirkbushell/eloquence
No additional configuration is required for Laravel 5.5+ (automatically discoverable).
First Use Case:
Enable camelCase attributes for JSON/API consistency by adding HasCamelAttributes to your model:
use Eloquence\Behaviours\Attributes\HasCamelAttributes;
class User extends Model {
use HasCamelAttributes;
}
Now, User::find(1)->toArray() returns ['firstName' => 'John'] instead of ['first_name' => 'John'].
Key Entry Points:
Attribute Transformation:
HasCamelAttributes for API responses or consistency with frontend code.HasSnakeAttributes for database storage (default behavior).AttributeTransformer for bespoke logic (e.g., HasKebabAttributes).// API Model (camelCase)
class ApiUser extends Model {
use HasCamelAttributes;
protected $casts = ['created_at' => 'datetime:Y-m-d'];
}
// Database Model (snake_case)
class User extends Model {
use HasSnakeAttributes;
}
Readonly Models:
HasReadOnly:
class AuditLog extends Model {
use HasReadOnly;
protected $fillable = []; // Explicitly block mass assignment
}
forceFill() or forceSave() for edge cases (documented in the package).Data Aggregation:
HasAggregates to preload computed fields (e.g., total_orders):
class Customer extends Model {
use HasAggregates;
protected $aggregates = [
'total_orders' => ['orders', 'count']
];
}
$customer->total_orders (lazy-loaded).Query Logging:
eloquence.log_queries = true) to debug slow queries or validate SQL.HasCamelAttributes with Laravel’s Resource classes for consistent JSON output.HasReadOnly for immutable test data (e.g., fixtures).HasTimestamps or HasUuids traits to standardize schema definitions.Readonly Models:
save(), update(), or fill() will throw ReadonlyException.
Fix: Use forceFill(['key' => 'value'])->save() or new static($attributes).isFillable() to allow specific fields:
public function isFillable(array $attributes) {
return in_array('metadata', $attributes);
}
Attribute Conflicts:
HasCamelAttributes with HasSnakeAttributes causes silent attribute mismatches.
Fix: Stick to one transformer per model hierarchy.Aggregates:
user.orders.user) cause infinite loops.
Fix: Use with() to eager-load relationships before accessing aggregates.Query Logging:
debugbar for conditional logging.dd($model->getAttributes()) to inspect raw vs. transformed values.config('eloquence.readonly') for global overrides.$aggregates match the model’s belongsTo/hasMany.Custom Transformers:
AttributeTransformer to add new cases (e.g., HasPascalAttributes):
class PascalTransformer extends AttributeTransformer {
public function transform($value, $key) {
return ucfirst(camel_case($key));
}
}
Readonly Exceptions:
throwReadonlyException() to customize error messages or redirect users.Aggregate Logic:
Aggregate class to support custom SQL or non-relational calculations.eloquence.readonly to true in config/eloquence.php to enforce readonly on all models.storage/logs/eloquent.log by default; adjust via eloquence.log_file.How can I help you explore Laravel packages today?