Forum

Ask, reply and learn. Join the community of Akaunting.

New Discussion

Extending core models

David Barnes   ( User )

Commented 3 years ago

How can I add methods to core models from within my module package?

For example, I want to add a simple accessor to App\Models\Banking\Account:

public function getSortCodeFormatAttribute(){
return implode("-", str_split($this->bank_sort_code, 2));
}

How can I add this without editing the `app/Http/Controllers/Banking/Accounts.php fiile`

David Barnes   ( User )

Commented 3 years ago

I've found 1 way to kind of do this, but it is not a good solution.

If i add something like this to my module "register()" method:

class_alias('Modules\BankingSortCode\Models\Account', 'App\Models\Banking\Account');

I can then copy the Account model into my package and add my own methods etc...

However this feels like a pretty dirty hack as it won't pickup any future changes made to the Account model in the core project. It would be much better to extend the original model or add a trait to it somehow.

Any ideas?

Denis Dulici   ( Admin )

Commented 3 years ago

Hooking models is possible but not in the way you want:

https://akaunting.com/docs/developer-manual/hooking-models

Please login or register to leave a response.

Showing 1 to 3 of 3 discussions