How to make my method execute a Closure

Hello, I'm writing a custom function inside my action which should accept a Closure.
public function autofill(array|Closure $data): void
{
$this->url(AssignmentResource::getUrl('create', $data));
}
public function autofill(array|Closure $data): void
{
$this->url(AssignmentResource::getUrl('create', $data));
}
Right now this gives the error, Object of class Closure could not be converted to string. Looking at other methods I couldn't find a difference, how do you do this?
4 Replies
Dennis Koch
Dennis Koch2y ago
if (is_callable($data) {
$data = $data();
}
if (is_callable($data) {
$data = $data();
}
Quadrubo
QuadruboOP2y ago
okay that kinda works but my Closure has the $livewire argument in it
Dennis Koch
Dennis Koch2y ago
So you are trying to replicate Filament style? Filament uses $this->evaluate($closure, []) for this case. Check whether that's available. Otherwise this is just a wrapper around the Laravel Container . app($closure, [your_arguments])
Quadrubo
QuadruboOP2y ago
ah that is it, thank you again!
Want results from more Discord servers?
Add your server