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\BaseModelPurpose
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
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:
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.
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:
- User-defined override from settings (
gss('package::model_name')) - Package-specific translation (e.g.,
wncms-faqs::word.faq) - Core translation (
wncms::word.post) - Fallback to class name (e.g.,
Post→Post)
public static function getModelName(?string $locale = null): stringExample:
Post::getModelName('zh_TW'); // → "文章"Package Namespace
getPackageId() allows a package to specify its namespace for translation or configuration isolation.
public static function getPackageId(): ?string
{
return property_exists(static::class, 'packageId')
? static::$packageId
: null;
}Example (in a package model):
class Product extends BaseModel
{
public static $packageId = 'wncms-ecommerce';
}Parent Model Name
Utility method to retrieve the short class name (without namespace):
public function getParentModelName(): string
{
return class_basename($this);
}Example:
$post = new \Wncms\Models\Post;
$post->getParentModelName(); // returns "Post"Summary
| Feature | Description |
|---|---|
| HasMultisite | Enables multi-site content separation |
| HasTags | Adds tagging, tag filters, and allowed tag types |
| TAG_TYPES support | Registers allowed tag types automatically |
| Attribute event hook | Allows runtime value modification via listeners |
| Localized model name | Returns translatable model name |
| Package awareness | Supports namespacing via packageId |
| Base for all models | Common layer for all WNCMS and custom models |
Recommended Usage
When creating a new model for your own module or package:
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.