Skip to content

HasFiles ​

Namespace: Unusualify\Modularous\Entities\Traits\HasFiles

Attaches files from the File model via a MorphToMany through the modularous_fileables pivot table. Locale-aware: each file attachment stores a role and locale pivot column, so a model can have different files per language.


Relationship ​

php
public function files(): MorphToMany

Pivot columns: role, locale. Ordered by pivot id ascending.


Methods ​

MethodSignatureDescription
file(string $role, ?string $locale = null, ?File $file = null): ?stringReturns the public URL for the file in the given role (and locale); falls back to fallback_locale when translatable.use_property_fallback is true
filesList(string $role, ?string $locale = null): arrayReturns an array of public URLs for all files in a role
fileObject(string $role, ?string $locale = null): ?FileReturns the raw File Eloquent model

Configuration ​

Config keyDefaultDescription
modularous.tables.fileables—Pivot table name for file attachments
translatable.use_property_fallbackfalseWhether to fall back to fallback_locale when no file exists for the requested locale
translatable.fallback_locale—Locale used when the primary locale file is missing

Usage ​

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

class Document extends Model
{
    use HasFiles;
}

// URL for the current locale
$document->file('attachment');

// URL for a specific locale
$document->file('brochure', 'fr');

// All files for a role
$document->filesList('downloads');
$document->filesList('downloads', 'de');

// Raw File model
$document->fileObject('contract');