php - Extend Laravel project model from within a package
Get the solution ↓↓↓I am using a Laravel based projected called Akaunting.
In the project it has models, for example they have a modelApp\Models\Banking\Account
I am trying to make aPackage
, and I have access only to add code into myPackage
(cannot edit files in the main Laravel project.
How can I extend theApp\Models\Banking\Account
model so that it has some additional methods; so that everywhere in the project where theApp\Models\Banking\Account
model is used the project will have access to my added methods.
Currently the only way I have been able to do this is by usingclass_alias
to completely replace the original model with my own custom model by adding this to myregister()
method in my package service provider:
class_alias('Modules\BankingSortCode\Models\Account', 'App\Models\Banking\Account');
But this means that I have to copy the entire contents ofApp\Models\Banking\Account
into my new customized model atModules\BankingSortCode\Models\Account
The problem with this is that if the original project updates theirApp\Models\Banking\Account
model then anyone running my package would not be using the updated model (and potentially break the whole site)
I want to be able to just extend the original model, but if I try doing that i get an Exception that I'm trying to re-declareApp\Models\Banking\Account
(see example of what I want to be able to do below):
<?php
namespace Modules\BankingSortCode\Models;
class Account extends \App\Models\Banking\Account {
public function getSortCodeFormatAttribute(){
return implode("-", str_split($this->bank_sort_code, 2));
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: failed to create image decoder with message 'unimplemented'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.