Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions database/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,27 +675,42 @@ Since models are [equipped to use behaviors](../services/behaviors), they can be
Inside the closure you can add relations to the model. Here we extend the `Backend\Models\User` model to include a profile (has one) relationship referencing the `Acme\Demo\Models\Profile` model.

```php
\Backend\Models\User::extend(function($model) {
\Backend\Models\User::extend(function ($model) {
$model->hasOne['profile'] = ['Acme\Demo\Models\Profile', 'key' => 'user_id'];
});
```

This approach can also be used to bind to [local events](#events), the following code listens for the `model.beforeSave` event.

```php
\Backend\Models\User::extend(function($model) {
\Backend\Models\User::extend(function ($model) {
$model->bindEvent('model.beforeSave', function() use ($model) {
// ...
});
});
```

### Extend after behaviors are loaded

It is also possible to have the provided callback execute after the model Behaviors have been loaded:

```php
\System\Models\MailSetting::extend(function ($model) {
if ($model->methodExists('getSettingsRecord')) {
// the SettingsModel behavior's getSettingsRecord() method is only available after the behavior has been loaded.
}
if ($model->getTable() === 'system_settings') {
// the model table changes from 'mail_settings' to 'system_settings' after the behavior has loaded
}
}, after: true);
```

> **NOTE:** Typically the best place to place code is within your plugin registration class `boot` method as this will be run on every request ensuring that the extensions you make to the model are available everywhere.

Additionally, a few methods exist to extend protected model properties.

```php
\Backend\Models\User::extend(function($model) {
\Backend\Models\User::extend(function ($model) {
// add cast attributes
$model->addCasts([
'some_extended_field' => 'int',
Expand Down