malsanyang/laravel-model-tools
Helper traits and utilities for Laravel Eloquent models that cut boilerplate around common model tasks, with a simple, convention-friendly API.
Laravel Model Tools v0.1.0 is a focused utility package for models that need a public UUID-style reference while keeping internal database IDs private. It requires PHP 8.3+ and Laravel 11, 12, or 13.
Start with the package README, then check the published/package configuration for the default reference column name, any model-specific overrides, and route model binding behavior. The migration generator is the next place to look because the trait expects the reference column to exist before models write references.
Minimal setup:
composer require malsanyang/laravel-model-tools:^0.1
php artisan model-tools:reference-migration pages
php artisan migrate
use Illuminate\Database\Eloquent\Model;
use MalSanyang\ModelTools\Concerns\HasReference;
class Page extends Model
{
use HasReference;
}
A good first use case is a Page model exposed at /pages/{page}: keep the internal id private, expose the UUID reference in URLs/API payloads, and let route model binding resolve by reference. For non-route lookups, use Page::referenceOrFail($reference) in controllers or services where a missing reference should produce a 404-style failure.
HasReference to models that are publicly addressable or externally referenced. This keeps the package’s behavior scoped and avoids surprising UUID generation on internal-only tables.php artisan model-tools:reference-migration <table> for each table that needs a reference column, inspect the generated migration, then migrate. Confirm the generated column is the intended name and has the appropriate unique index for your data.reference and hide/omit id./pages/{page}, and ensure the binding configuration and model’s route key point at the same reference column. Verify 404 behavior for unknown or malformed references.Model::reference($reference) when a missing result is acceptable, and Model::referenceOrFail($reference) when the caller expects a model or a failure. Prefer referenceOrFail() in controller actions that should not continue with null.BaseModel. The package provides an optional base model with HasReference, soft deletes, and datetime casts. Use it only if that inheritance is a good fit; otherwise the trait is the less invasive choice.referenceOrFail() 404 behavior, JSON output containing reference but not id, and behavior for soft-deleted records if using the optional BaseModel.^0.1 and review the README/changelog before upgrading. Expect the public API and configuration details to evolve more than in a mature package.reference out of $fillable unless you have a deliberate, controlled use case.BaseModel with soft deletes, decide whether public routes should resolve trashed models. Use explicit withTrashed() behavior only where intended.composer dump-autoload.reference() vs referenceOrFail(). Use the first only when null is meaningful; use the second for controller/service paths where a missing model should fail fast.How can I help you explore Laravel packages today?