To define new relations on Waterhole's models, use the resolveRelationUsing method, typically in the boot method of a service provider:
use App\Models\Address;
use Waterhole\Models\User;
User::resolveRelationUsing('address', function (User $user) {
return $user->belongsTo(Address::class, 'address_id');
});
You can listen for the standard events dispatched by Eloquent models to hook into the following moments in a model's lifecycle: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored, and replicating.
use Waterhole\Models\User;
User::created(function (User $user) {
// ...
});
Waterhole models also dispatch an initialized event whenever a new model instance is constructed. This can be useful if you need to define new attribute casts, or set the default value of an attribute:
use App\Models\Address;
use Waterhole\Models\User;
User::initialized(function (User $user) {
$user->mergeCasts(['subscribed_at' => 'datetime']);
});