how to override a default database model class method
Hi team,
I’d like to get your opinion on overriding a specific class in Twill.
Current approach:
Copy the model's content, extend the original model class, and then reference the newly extended class.
Is this the correct approach?
Let me know your thoughts.
16 Replies
DO we have any better approach?
Problem with current approach is updating twill from 2 to 3 is too difficult.
Hi @Albert which class are you talking about?
@ifox
Block Eloquent model class
WIll add an additional method in it
Are you using the
twill.models.block
config to do that? If you are just trying to add a method you can extend it, you don't need to copy the whole fileI don't find models.block in twill config..
So I must mention the model name in the config, then extend the A17\Twill\Models\Block class to my custom class.
Can I place the custom class anywhere in the application?
GitHub
twill/config/models.php at cb834690e45a0b471a55479feb01cf460a82af51...
Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/c...
Yes you can place it anywhere as long as you respect psr4 namespaces
Wow, great, thanks for the details.
Will it be same for A17\Twill\Repositories\BlockRepository as well?
there's isn't any configuration based way for the repository class, but you can easily do it by binding your own class into the container, like this: https://github.com/area17/twill/discussions/1232
GitHub
Overriding Twill internal classes · area17 twill · Discussion #1232
Hey! This won't be groundbreaking for experienced Laravel developers but I've just realized probably the easiest (and safest) way to override some of Twill's internals through dependenc...
Hello Again,
A17\Twill\Models\Media model has scopeUnused method, I would like to override the same in my custom class
App\TwillOverrides\Models\Media which extends the base media class (Above mentioned class)
in app service provider
$this->app->bind(A17\Twill\Models\Media::class, App\TwillOverrides\Models\Media);
It works fine
From yesterday's conversation I have created a models config with the following content
'media' => \A17\Twill\Models\Media::class, But still it refers to the base class instead of override class. tried with the override class as well, but no luck 'media' => \App\TwillOverrides\Models\Media::class tried with clearing cache too, not sure what Im missing
'media' => \A17\Twill\Models\Media::class, But still it refers to the base class instead of override class. tried with the override class as well, but no luck 'media' => \App\TwillOverrides\Models\Media::class tried with clearing cache too, not sure what Im missing
I didnt check the code, but if somewhere hardcoded model was used instead of config one, your override wont work.
What you can do (but this can cause issues later with the upgrade) is the composer override.
I used it for my usecase and here is the short snippets.
In my own code I copied the Twill Media model and use it like it comes from the Twill repo.
I am using twill 2 and soon will upgrade to 3. I have faced many challenges during the previous upgrade of twill. So looking for a elegant override mechanism for vue files, models, controllers, blade files, services, repositories
@kallefrombosnia Thanks for the suggestion.
Have you ever used config.models file?
Hi @Albert the media model isn't part of that configuration at the moment. It could definitely be.
I'm sorry to hear you have faced many challenges in the past. Please don't hesitate to ask for support here and report issues with overrides that would benefit the community.
I didnt, like @ifox said, that model file isnt in the configuration. But deffinetely it could be done via PR and proper testing.
@ifox which means models config feature is available from version 3 and not in version 2 !!! Or its available in version 2, but media model is not configurable at the moment? if so what all models supported?
It's a v3 feature. the supported models are in the configuration file I previously shared