tusimo/eloquent-default
A Laravel package that lets Eloquent models declare default attribute values, reducing boilerplate and keeping newly created records consistent without repeating assignments across controllers and services.
This is an early v0.1 package, so start by reading the service provider and any config file it publishes before wiring it into models.
tinker check:$model = new App\Models\Post;
// Confirm the package can resolve defaults and columns
dump($model->getFillable()); // sanity check
dump($model->toArray()); // see whether default values are applied
Where to look first:
config/ for connection or column-resolution options.Model defaults
Use the package to declare default attribute values at the model level instead of scattering them across controllers, migrations, and factories. This is especially useful for Mongo models, where schema-level defaults are less available than in relational databases.
class Post extends Model
{
use HasPackageDefaults;
protected $defaults = [
'status' => 'draft',
'published_at' => null,
];
}
Then create records without passing every attribute:
$post = Post::create(['title' => 'Hello']);
// status falls back to 'draft'
Saving columns data
Use the package’s column metadata when constructing payloads dynamically — for example, a generic CRUD form or an API endpoint that accepts a subset of columns.
$columns = $model->getColumns(); // package-provided column data
$payload = collect($request->only(array_keys($columns)))
->filter(fn ($value) => $value !== null)
->all();
$model->fill($payload)->save();
This keeps validation, form rendering, and persistence aligned to a single source of truth.
Integration tips
composer.json.$fillable/$guarded. If a defaulted attribute is not fillable, it may still be set by the package — confirm this matches your security expectations.How can I help you explore Laravel packages today?