F
Filament7mo ago
Harvey

Reuse macro callbacks for multiple classes

Is there a way of extracting the anonoymous functions to a class function? I would like it so I can just pass $this->onlyForAdmins, or [$this, 'onlyForAdmins'] and not have to duplicate the func body every time.
Field::macro('onlyForAdmins', function () {
return $this->visible(function () {
return Auth::user()->is_admin;
});
});

MountableAction::macro('onlyForAdmins', function () {
return $this->visible(function () {
return Auth::user()->is_admin;
});
});

ActionGroup::macro('onlyForAdmins', function () {
return $this->visible(function () {
return Auth::user()->is_admin;
});
});
Field::macro('onlyForAdmins', function () {
return $this->visible(function () {
return Auth::user()->is_admin;
});
});

MountableAction::macro('onlyForAdmins', function () {
return $this->visible(function () {
return Auth::user()->is_admin;
});
});

ActionGroup::macro('onlyForAdmins', function () {
return $this->visible(function () {
return Auth::user()->is_admin;
});
});
I tried to just extract it like I mentioned, but it complains that $this->visible() is not a method on the class. Is this a Laravel macro thing?
Solution:
Found you can just macro a more general class ViewComponent that's inherited by Field, Column, Entry etc. Not perfect but it'll work.
Jump to solution
1 Reply
Solution
Harvey
Harvey7mo ago
Found you can just macro a more general class ViewComponent that's inherited by Field, Column, Entry etc. Not perfect but it'll work.
Want results from more Discord servers?
Add your server