NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Attributes for Laravel

The fluent builder

Attributes::for($model)->stage() returns a fluent writer — $model->attributes() is the same writer as model shorthand. Stage typed values and metadata, then persist them in one readable statement:

use RoundlyConsulting\Attributes\Facades\Attributes;

Attributes::for($product)->stage()
    ->set('color', 'white')->meta('color', ['hex' => '#fff'])
    ->set('rating', 5)
    ->set('on_sale', true)
    ->save();

// Model shorthand: $product->attributes() is Attributes::for($product)->stage()
$product->attributes()->set('stock', 8)->save();

Save or sync

  • save() — attaches the staged attributes through the handle’s setMany() and keeps any others already on the model. With nothing staged it does nothing.
  • sync() — goes through the handle’s sync(): attaches the staged attributes and removes every attribute that was not staged. Pass forceDelete: true to hard-delete the removed rows. With nothing staged it removes every attribute.
// save(): add or update the staged attributes, keep the rest
$product->attributes()
    ->set('stock', 8)
    ->save();

// sync(): the staged set becomes the complete set
$product->attributes()
    ->set('color', 'black')
    ->set('rating', 4)
    ->sync();                      // every other attribute is soft-deleted

$product->attributes()
    ->set('color', 'black')
    ->sync(forceDelete: true);     // ...or hard-deleted

Adjusting the staged set

forget() and except() drop staged names; only() keeps just the names you list. Metadata staged for a dropped name is dropped with it, and metadata staged for a name with no staged value is not persisted:

$writer = $product->attributes()
    ->set('color', 'white')->meta('color', ['hex' => '#fff'])
    ->set('size', 'large')
    ->set('internal_note', 'check supplier');

$writer->forget('internal_note');   // drop one or more staged names
$writer->except('size');            // alias of forget()
$writer->only('color', 'size');     // keep just these names

$writer->save();                    // persists color (with its meta)

meta() accepts an array or a Collection. Each staged attribute goes through the same strict-mode, validation, uniqueness, history and event pipeline as set(), and every staged value is validated before the first write — one invalid value leaves the owner untouched. Both save() and sync() return the model.

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.