Replies: 3 comments 8 replies
-
Hi, Update. class CustomCast implements CastsAttributes
{
protected SomeService $service;
public function __construct(SomeService $service)
$this->service = $service;
{
}
public function get($model, $key, $value, $attributes)
{
// Some logic using $this->service
}
public function set($model, $key, $value, $attributes)
{
// Some logic using $this->service
}
} |
Beta Was this translation helpful? Give feedback.
-
Not sure if casts are resolved using the service container, but you can resolve it using the |
Beta Was this translation helpful? Give feedback.
-
I found the answer to my question and casts do not allow dependency injection in the constructor this is seen in trait HasAttributes Does anyone have any ideas on how to allow Laravel to do dependency injection in casts, it seems convenient to me and this behaviour would be expected as in other parts of my application I can freely use the dependency injection container in the constructor |
Beta Was this translation helpful? Give feedback.
-
Hi everyone,
I'm trying to use dependency injection in the constructor of a custom cast in my Laravel application. However, it seems like the usual approach for injecting dependencies isn't working here.
For example:
When I attempt this, Laravel throws an error, as it doesn't seem to resolve the dependencies automatically.
Are there workarounds or best practices for achieving this in custom casts without resorting to calling the container manually using the
app()
helper function?Thanks in advance for any insights!
Beta Was this translation helpful? Give feedback.
All reactions