Skip to content

HasCreator ​

Namespace: Unusualify\Modularous\Entities\Traits\HasCreator

Records which authenticated user created the model via a CreatorRecord morph. Supports custom creator overrides for admin-on-behalf-of workflows. Integrates with Spatie Permissions for company-level access control.


Boot Behavior ​

EventAction
savingDetects custom_creator_id override; clears fillable helpers
saved (new)Creates CreatorRecord for current auth user (or custom creator if set)
saved (update)Updates CreatorRecord if a custom creator was set
deleting / forceDeletingDeletes the associated CreatorRecord

Relationships ​

php
public function creatorRecord(): MorphOne     // → CreatorRecord
public function creator(): HasOneThrough      // → User through CreatorRecord

Fillable Helpers ​

FieldDescription
custom_creator_idOverride the auto-detected creator user ID
custom_creator_typeOverride the creator model class
custom_guard_nameOverride the guard name stored on the record

Computed Attributes ​

AttributeTypeDescription
creator_record_existsboolPre-computed from withExists('creatorRecord') global scope

Scopes ​

ScopeDescription
scopeIsCreator($creator_id, $guardName)Records created by a specific user ID and guard
scopeIsMyCreation($user, $guardName)Records created by the authenticated user
scopeHasAccessToCreation($user, $guardName)Records the user can access: own creations + company-scoped
scopeAuthorized($guardName)(deprecated) Use scopeHasAccessToCreation

Methods ​

MethodSignatureDescription
getCreatorModel(): stringReturns the creator model class (from CreatorRecord or default)
getDefaultCreatorModel(): string (static)Returns the default creator model (override via $defaultHasCreatorModel)
getRolesHasAccessToCreation(): arrayReturns roles that see all records (bypass ownership filter)
getCompanyRolesHasAccessToCreation(): arrayReturns company-level roles that see company-scoped records

Global Scopes ​

Registers creator_record_exists via addGlobalScopesHasCreator().


Configuration ​

PropertyTypeDefaultDescription
$defaultHasCreatorModelstringUser::classCreator model class
$rolesHasAccessToCreationarray['admin','manager','editor']Roles that bypass ownership filter
$companyRolesHasAccessToCreationarray['manager','client-manager']Roles that see company-wide records

Usage ​

php
use Unusualify\Modularous\Entities\Traits\HasCreator;

class Article extends Model
{
    use HasCreator;
}

// Read creator
$article->creator;              // User who created this
$article->creatorRecord;

// Filter queries
Article::isMyCreation()->get();
Article::isCreator($userId)->get();
Article::hasAccessToCreation()->get(); // current user's accessible records

// Override creator (e.g. admin creating on behalf of client)
$article->custom_creator_id = $clientUser->id;
$article->save();