Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Eloquent Default Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

  1. Install the package with Composer and publish its config if the install instructions call for it.
  2. Add the package trait (or implement the expected interface) on a model you want to expose defaults and column metadata for.
  3. Run a quick 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
  1. First real use case: define sensible defaults on an Eloquent model (or a Mongo-backed model) and use the package’s column data to build a save payload for a form, API request, or seeder.

Where to look first:

  • config/ for connection or column-resolution options.
  • The trait added to your model — its methods are the main extension surface.
  • The README’s Mongo example, since v0.1 explicitly targets Mongo models.

Implementation Patterns

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

  • Pair column data with Laravel validation rules generated from the same metadata where possible.
  • Use defaults in factories and seeders so test data matches production behaviour.
  • For Mongo models, verify that defaults are applied on insert rather than only when reading.

Gotchas and Tips

  • v0.1 API instability: This is a first release. Treat trait names, method signatures, and config keys as provisional and pin the version in composer.json.
  • Mongo-specific behaviour: Defaults that work for Eloquent may behave differently for Mongo models. Test inserts and upserts explicitly.
  • Defaults vs. mass assignment: Defaults do not bypass $fillable/$guarded. If a defaulted attribute is not fillable, it may still be set by the package — confirm this matches your security expectations.
  • Columns data is not validation: The column payload tells you what can be saved, not what is valid. Keep separate validation rules.
  • Debugging: If defaults are not appearing, check the trait is actually used on the model and that the package’s service provider is registered. Dump the resolved column array before saving.
  • Extension points: Look for overridable methods on the trait for resolving defaults and column metadata. These are the safest places to customise behaviour without forking.
  • Connection quirks: If the package reads schema information, ensure the model’s connection is configured before resolving columns — especially in queue workers or multi-connection apps.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc