Filament Resources: Use Static Methods to Reuse Code

If you create a property in the Resource class to reuse it in methods, make it STATIC.

Both form() and table() methods are static, so if you use something like $this->options, it will throw an error.

Create a static property and then use it as self::$options

class ProductResource extends Resource
{
protected array $options;
protected static array $staticOptions;
 
public static function form(Form $form): Form
{
$options = $this->options; // will throw error
$options = self::$staticOptions; // this will works
}
 
public static function table(Table $table): Table
{
$options = $this->options; // will throw error
$options = self::$staticOptions; // this will works
}
}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1056 lessons, total 44 h 09 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials