Skip to content

Models

Overview

Eloquent models are organized by domain under app/Models/. Each domain directory contains the model file and optionally a Concerns/ subdirectory for traits.


Folder Structure

app/Models/
├── Academic/                # Academic domain
│   ├── AcademicYear.php
│   ├── SchoolClass.php
│   └── Concerns/
│       ├── HasRelations.php
│       └── HasScopes.php
├── Recruitment/             # Recruitment domain
│   ├── Vacancy/Vacancy.php
│   ├── Applicant/Applicant.php
│   └── ...
├── Platform/                # Shared/platform models
│   ├── User.php
│   ├── Asset/Asset.php
│   └── Setting/Setting.php
├── Institution/
├── Financial/
├── Geographic/
├── News/
├── Organization/
├── Support/
├── Assistant/
└── ContractManagement/

Conventions

ConventionRuleExample
NamespaceApp\Models\{Domain}\{Model}App\Models\Recruitment\Candidate
Table nameSnake_case plural of modelcandidates
Primary keyUUID (v4)$table->uuid('id')->primary()
TimestampsAlways included$table->timestamps()
Soft deletesWhen applicableuse SoftDeletes

Common Traits

Traits shared across models live in app/Traits/Common/:

TraitPurposeUsage
HasLocalizationResolve localized JSON columnsprotected array $localizations = ['name']
HasContactAttach contacts via master tableprotected array $contacts = ['phone', 'email']
HasLogAutomatic activity loggingNo config needed — just use HasLog
HasAssetDynamic asset relationshipsprotected array $assets = ['logo_asset_id']
HasUuidsAuto-generate UUID primary keyInherited from base model

Trait Usage Example

php
namespace App\Models\School;

use App\Traits\Common\HasLocalization;
use App\Traits\Common\HasContact;
use App\Traits\Common\HasLog;
use Illuminate\Database\Eloquent\Model;

class School extends Model
{
    use HasLocalization, HasContact, HasLog;

    protected array $localizations = ['name', 'description'];
    protected array $contacts = ['phone', 'email', 'whatsapp'];
}

Concerns Pattern

For complex models, domain-specific logic is extracted into Concerns/ traits:

Model.php
Concerns/
├── HasAttributes.php    # Accessors, mutators, casts
├── HasRelations.php     # Relationship definitions
└── HasScopes.php        # Query scopes

Creating a New Model

bash
php artisan make:model Models/Domain/ModelName --all

This generates the model, factory, seeder, migration, policy, and form request. Move the migration to the appropriate version directory afterward.


Key Files

FilePurpose
app/Models/{Domain}/*.phpDomain models
app/Traits/Common/Shared traits (HasLocalization, HasContact, HasLog, HasAsset)
app/Models/Platform/User.phpUser model (authentication, roles)