This package centers around ModelController, which powers TranquilController and TranquilInertiaController. It implements the full RESTful resource flow and adds helpful utilities for relations, files, policies, and list filtering.
1use Tranquil\Controllers\TranquilController;2 3class CarController extends TranquilController {4 protected string $modelClass = Car::class; // define your model class5}
These methods are automatically implemented by TranquilController:
If your model extends TranquilModel or uses the HasValidation trait, inputs are validated automatically using rules returned by getValidationRules() and default attributes from getDefaultValidationAttributes().
ModelController understands nested payloads for the following relations, both on create and update:
{ relation: { id: 123 } }
to associate, or { relation: null }
to disassociate.{ relation: { id: 456, ...attributes } }
to create/update. Use { relation: null }
to remove association.{ relation: [{ id: 1, ...}, { id: null, ...new }, ...] }
. Include an _delete: true
flag on a child to delete it when $deletableAsHasMany
is true on the child model.{ relation: [{ id: 5, pivot: { key: value }}, ...] }
. Items without id
are created if the child is creatable.ModelController integrates with policies via HasPolicy trait on your models and the controller’s checkModelPolicy() method. Ensure your user model policies allow/deny actions and the controller will enforce them during operations.
If your model uses HasAttachments, ModelController’s saveFiles() and saveAttachments() will persist uploaded files and create Attachment records accordingly.
The controller constructs parameter arrays for each response type and can load relations/appends dynamically. See methods like getLoadRelations(), getLoadAppends(), getCreateEditParametersWithModel().