Dependency injection #16405
Replies: 1 comment
-
Yes, it's possible to use constructors in your Filament classes, but with a bit of caution. Since Filament is built on top of Livewire, which has its own component lifecycle, overriding the constructor ( So what’s the right way? The recommended approach is to use the Example using protected MyService $myService;
public function mount(MyService $myService)
{
$this->myService = $myService;
} Or in protected MyService $myService;
public function boot(MyService $myService)
{
$this->myService = $myService;
} This keeps things clean and fully compatible with Livewire’s lifecycle. What if I really want to use the constructor? You technically can, but you must call public function __construct(MyService $myService)
{
$this->myService = $myService;
parent::__construct(); // Don't forget this!
} That said, unless there's a very specific reason, it’s better to avoid this and just stick with Bonus Tip: If you just need a quick service instance, you can always do: $this->myService = app(MyService::class); TL;DR Laravel can handle DI in Filament classes — just inject your dependencies into |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Package
Other
Package Version
v3.2.96
How can we help you?
Is it possible to crate constructors in my filament classes so laravel can deal with dependecy injection?
Beta Was this translation helpful? Give feedback.
All reactions