Skip to content

Base Model

BaseModel is the foundation class for all models in WNCMS. It extends Laravel’s native Illuminate\Database\Eloquent\Model and provides additional features such as multi-site support, tag handling, dynamic model naming, and attribute event hooks.


Location

Wncms\Models\BaseModel

Purpose

All WNCMS models (and your custom models) should extend this class to inherit:

  • Multi-site awareness through HasMultisite
  • Tag relationship and filtering via HasTags
  • Automatic tag-type registration
  • Event dispatching when accessing attributes
  • Translatable model name resolution
  • Package-aware namespace handling

Traits Used

HasMultisite

Adds support for multi-website data isolation. Each record can belong to a specific website when multi_website mode is enabled.

HasTags

Provides a unified interface for tag assignment and filtering. Supports multiple tag types such as post_category, link_category, etc.


Boot Logic

php
protected static function booted()
{
    if (defined(static::class . '::TAG_TYPES')) {
        (new static())->addAllowedTagTypes(static::TAG_TYPES);
    }
}

When a model defines a TAG_TYPES constant, it automatically registers those tag types during boot. This allows you to define allowed tag types easily in your custom models:

php
class Product extends BaseModel
{
    const TAG_TYPES = ['product_category', 'product_tag'];
}

Attribute Event Hook

BaseModel overrides getAttribute() to dispatch a custom event whenever an attribute is retrieved.

php
public function getAttribute($key)
{
    $value = parent::getAttribute($key);

    if (array_key_exists($key, $this->attributes)) {
        $event = new \Wncms\Events\ModelGettingAttribute($this, $key, $value);
        event($event);
        $value = $event->value;
    }

    if ($this->hasCast($key)) {
        if (is_string($value) || is_null($value)) {
            return $this->castAttribute($key, $value);
        }
        return $value;
    }

    return $value;
}

This event-based approach allows listeners (e.g., keyword replacers, filters, plugins) to modify field values at runtime without altering database data.


Model Name Resolution

getModelName() returns a localized display name for the model based on several fallbacks:

  1. User-defined override from settings (gss('package::model_name'))
  2. Package-specific translation (e.g., wncms-faqs::word.faq)
  3. Core translation (wncms::word.post)
  4. Fallback to class name (e.g., PostPost)
php
public static function getModelName(?string $locale = null): string

Example:

php
Post::getModelName('zh_TW'); // → "文章"

Package Namespace

getPackageId() allows a package to specify its namespace for translation or configuration isolation.

php
public static function getPackageId(): ?string
{
    return property_exists(static::class, 'packageId')
        ? static::$packageId
        : null;
}

Example (in a package model):

php
class Product extends BaseModel
{
    public static $packageId = 'wncms-ecommerce';
}

Parent Model Name

Utility method to retrieve the short class name (without namespace):

php
public function getParentModelName(): string
{
    return class_basename($this);
}

Example:

php
$post = new \Wncms\Models\Post;
$post->getParentModelName(); // returns "Post"

Summary

FeatureDescription
HasMultisiteEnables multi-site content separation
HasTagsAdds tagging, tag filters, and allowed tag types
TAG_TYPES supportRegisters allowed tag types automatically
Attribute event hookAllows runtime value modification via listeners
Localized model nameReturns translatable model name
Package awarenessSupports namespacing via packageId
Base for all modelsCommon layer for all WNCMS and custom models

When creating a new model for your own module or package:

php
namespace App\Models;

use Wncms\Models\BaseModel;

class Article extends BaseModel
{
    const TAG_TYPES = ['article_category', 'article_tag'];

    public static $packageId = 'my-custom-package';
}

This ensures your model automatically inherits WNCMS’s tagging, multi-site, and event capabilities.

Built with ❤️ for WNCMS