code16/sharp
Sharp is a Laravel 11+ (PHP 8.3+) content management framework for building a CMS/admin area with a clean UI and great DX. Code-driven, data-agnostic, no frontend to write. Manage structured data with search, filters, commands, auth, and validation.
Sometimes you will need to configure a "unique" resource that does not fit into a List / Form schema, like for example an account, or a configuration item. Single Forms are the natural companions for Single Shows, documented here.
Instead of extending SharpForm, our SingleForm implementation should extend Code16\Sharp\Form\SharpSingleForm. We still have to implement buildFormFields(FieldsContainer $formFields) and buildFormLayout(FormLayout $formLayout) to declare the fields presenting the instance, but other methods are a bit different. First, find() and update() don't need any $instanceId parameter:
findSingle(): arrayupdateSingle(array $data)Let's write a Single Form for the current User, where he can update its name and email (using WithSharpFormEloquentUpdater here as this example uses Eloquent):
class AccountSharpForm extends SharpSingleForm
{
use WithSharpFormEloquentUpdater;
function buildFormFields(FieldsContainer $formFields): void
{
$formFields
->addField(
SharpFormTextField::make('name')
->setLabel('Name')
)
->addField(
SharpFormTextField::make('email')
->setLabel('Email address')
);
}
function buildFormLayout(FormLayout $formLayout): void
{
$formLayout->addColumn(6, function ($column) {
return $column
->withField('name')
->withField('email');
});
}
protected function findSingle()
{
return $this->transform(
User::findOrFail(auth()->id())
);
}
protected function updateSingle(array $data)
{
return $this->save(
User::findOrFail(auth()->id()),
$data
)->id;
}
}
Like said before, Single Forms will only work in pair with a Single Show; please refer to this documentation to find out how to declare a single show and form.
How can I help you explore Laravel packages today?