Singular ​
The Singular feature enforces a single record per type (singleton pattern). It is entity-only — no repository trait or Hydrate.
Entity Trait: IsSingular ​
Add the IsSingular trait to your model:
php
<?php
namespace Modules\Settings\Entities;
use Unusualify\Modularous\Entities\Model;
use Unusualify\Modularous\Entities\Traits\IsSingular;
class SiteSettings extends Model
{
use IsSingular;
protected $fillable = ['site_name', 'site_description', 'contact_email'];
}How It Works ​
- Uses a global scope
SingularScopeso only one record exists per type - Stores data in a
modularous_singletonstable (configurable viatables.singletons) singleton_typestores the model classcontentstores fillable attributes as JSON (excludingsingleton_type,content)
Methods ​
- single() — Returns the singleton record (creates if none exists)
- scopePublished — Filters by
content->published - isPublished() — Returns whether the record is published
Boot Logic ​
- On creating: Sets
singleton_type; moves fillable attributes intocontent; unsets fillable from attributes - On updating: Same as creating
- On retrieved: Loads
contentback into attributes; unsetscontentandsingleton_type
Example ​
php
$settings = SiteSettings::single();
$settings->site_name = 'My Site';
$settings->save();Repository Trait ​
None. The singleton is managed at the entity level.
Input Config ​
None. Use standard form inputs for the model's fillable attributes; the form targets the singleton route.
Hydrate ​
None.