Replies: 1 comment 3 replies
-
I think this is not possible, due to the dependency injection from the container. This doesn't work in regular controllers also, it just resolves the model. You can work around it by either check if it's empty or remove the type from the definition, like so: class MyComponent extends Component
{
public ?MyModel $myModel = null;
public function mount($myModel = null): void
{
dd($myModel); // should be null now
$this->myModel = $myModel;
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have the problem using a component with Livewire, but after analyzing the code flow, I have discovered that where the unexpected occurs is in a native Laravel class.
The problem
I have a class attribute of type MyModel|null.
In the mount() method of a Livewire component, I expect to receive a parameter of type MyModel|null and default to null. The problem occurs when in the component call, no argument is passed.
I would expect to receive the parameter as null since in the definition of the parameter's data type it could be null.
However, what I get is an object of type 'MyModel' with no attributes.
Solutions?
What is the reason for this? If the parameter was not nullable, I understand that my attribute would be of type MyModel without attributes. But, however, I would expect to get a null.
Example code
Call to the component without argument.
<livewire:my-component />
In the component class
I fact, made an example at Wirebox
Beta Was this translation helpful? Give feedback.
All reactions