diff --git a/database/data/permissions.php b/database/data/permissions.php index ac11dc7d0c..5e8e69e619 100644 --- a/database/data/permissions.php +++ b/database/data/permissions.php @@ -7,8 +7,8 @@ return [ | Permissions |-------------------------------------------------------------------------- | - | The initial system permissions needed by Filament. These are create using - | the `php artisan filament:install` command. + | The initial system permissions needed by Filament. These are added when + | running the initial seed `php artisan db:seed --class=FilamentSeeder` | */ diff --git a/resources/stubs/fieldset.stub b/resources/stubs/fieldset.stub index 5f3cd765da..4d2318d7a5 100644 --- a/resources/stubs/fieldset.stub +++ b/resources/stubs/fieldset.stub @@ -20,6 +20,10 @@ class DummyFieldset /** * Return the fields for the given form. * + * Note: these are core database columns defined by the model schema. + * If you add a field, first create a migration and then add the referring field here. + * If you want to attach arbitrary data then add the field to the `metaFields()` method defined below. + * * @var object $model * @return array */ @@ -32,6 +36,25 @@ class DummyFieldset ]; } + /** + * Return the meta data fields for the given form. + * + * This will allow for easily attaching arbitrary custom fields + * without needing to adjust the database schema. + * + * @var object $model + * @return array + */ + public static function metaFields($model) + { + return [ + Field::make('meta-field') + ->input() + ->label('A Custom Meta Field') + ->rules(['string', 'max:255']), + ]; + } + /** * Return the realtime rules to ignore for the given form. * diff --git a/src/Traits/HasForm.php b/src/Traits/HasForm.php index 589c2a8743..8b2b0f1a4f 100644 --- a/src/Traits/HasForm.php +++ b/src/Traits/HasForm.php @@ -152,7 +152,24 @@ trait HasForm } /** - * Return fields as a collection from a given fieldset. + * Return realtime rules to ignore from a given fieldset. + * + * @return array + */ + public function rulesIgnoreRealtime() + { + $rules = []; + if ($fieldset = $this->fieldset()) { + if (method_exists($fieldset, 'rulesIgnoreRealtime')) { + $rules = call_user_func("{$fieldset}::rulesIgnoreRealtime"); + } + } + + return $rules; + } + + /** + * Return the fields as a collection from a given fieldset. * * @return Illuminate\Support\Collection */ @@ -169,7 +186,7 @@ trait HasForm } /** - * Get a field from a fields collection. + * Get a field from the fields collection. * * @var string $field_name * @return null|object @@ -182,19 +199,32 @@ trait HasForm } /** - * Return realtime rules to ignore from a given fieldset. + * Return the meta fields as a collection from a given fieldset. * - * @return array + * @return Illuminate\Support\Collection */ - public function rulesIgnoreRealtime() + public function metaFields() { - $rules = []; + $metaFields = []; if ($fieldset = $this->fieldset()) { - if (method_exists($fieldset, 'rulesIgnoreRealtime')) { - $rules = call_user_func("{$fieldset}::rulesIgnoreRealtime"); + if (method_exists($fieldset, 'metaFields')) { + $metaFields = call_user_func("{$fieldset}::metaFields", $this->model); } } - return $rules; + return collect($metaFields); + } + + /** + * Get a meta field from the metaFields collection. + * + * @var string $field_name + * @return null|object + */ + public function getMetaField(string $field_name) + { + return $this->metaFields()->filter(function ($metaField, $key) use ($field_name) { + return $metaField->name === $field_name; + })->first(); } } \ No newline at end of file