How do I add model settings to a model?
Model settings provide a consistent way to store configurable settings against an Eloquent model. There are various steps needed to be followed to setup model settings for a model.
First, add the \Aero\Common\Traits\CanHaveSettings to the model and optionally define a RELATION_KEY:
class Acme extends Model
{
use \Aero\Common\Traits\CanHaveSettings;
public const RELATION_KEY = 'acme' //optional
//...
}
If you added a RELATION_KEY make sure to add it to the relation morph map.
Then update create/update requests for this model to wrap the existing rules with ::getSettingRules():
class CreateAcmeRequest extends AeroRequest
{
public function rules(): array
{
return Acme::getSettingRules([
//...
]);
}
}
This adds the validation rules required by the model settings alongside the request's normal validation rules.
Finally, after validating the requests, save the model using saveSettings():
$data = $request->validated();
$acme->saveSettings($data['settings'] ?? [], true);
For UI support, you must include the model settings partial in your views:
@include('admin::settings.model-settings', ['model' => $acme ?? new Acme()])