Skip to content

HasCrudOperations Trait

Anil\FastApiCrud\Concerns\HasCrudOperations

Shared CRUD query building, data operations, and lifecycle hooks. Used by both BaseController and BaseWebController.

Properties

PropertyTypeDefaultDescription
$paginationTypePaginationTypeLengthAwarePagination strategy
$scopesarray[]Scopes for index query
$loadScopesarray[]Scopes for show query
$witharray[]Eager loads for index
$withCountarray[]Count relations for index
$withAggregatearray[]Aggregates for index
$loadarray[]Eager loads for show
$loadCountarray[]Count relations for show
$loadAggregatearray[]Aggregates for show
$forceDeleteboolfalseForce permanent deletion
$deleteScopesarray[]Scopes for delete operations
$columnScopesarray[]Scopes for status/column ops
$restoreScopesarray[]Scopes for restore operations
$updateScopesarray[]Scopes for update operations
$modelModelResolved model instance
$storeRequeststringStore FormRequest class
$updateRequeststringUpdate FormRequest class

Query Builders

buildIndexQuery

php
protected function buildIndexQuery(): Builder

Builds the index query: initializer() → eager loads → counts → aggregates → scopes → search.

buildShowQuery

php
protected function buildShowQuery(): Builder

Builds the show query: initializer() → eager loads → counts → aggregates → scopes.

Perform Methods

All throw exceptions on failure (caught by the calling controller). Operations run inside database transactions.

MethodReturnsDescription
performStore()ModelValidate, create, hooks
performUpdate($id)ModelValidate, update, hooks
performDestroy($id)voidDelete (soft/force), hooks
performBulkDelete()voidValidate IDs, delete each, hooks
performChangeStatus($id, $column)ModelToggle 0/1, hooks
performUpdateColumn($id, $column)ModelUpdate from request, hooks
performRestore($id)ModelRestore trashed, hooks
performRestoreAll()voidRestore all trashed
performPermanentDelete($id)voidForce delete trashed, hooks

Lifecycle Hooks

See Lifecycle Hooks guide.

HookCalled By
beforeCreate(Model) / afterCreate(Model)performStore()
beforeUpdate(Model) / afterUpdate(Model)performUpdate()
beforeDelete(Model) / afterDelete(Model)performDestroy(), performBulkDelete()
beforeStatusChange(Model) / afterStatusChange(Model)performChangeStatus()
beforeColumnUpdate(Model) / afterColumnUpdate(Model)performUpdateColumn()
beforeRestore(Model) / afterRestore(Model)performRestore()
beforeForceDelete(Model) / afterForceDelete(Model)performPermanentDelete()

Internal Helpers

MethodReturnsDescription
resolveModel($class)ModelValidate and instantiate model
resolveValidatedData($requestClass)array<string, mixed>Resolve FormRequest, return only fillable data
resolveFormRequest($class, $name)stringValidate FormRequest class
resolveResource($class)stringValidate JsonResource class
findModel($id, $scopes)ModelFind by ID with optional scopes
applyScopes($query, $scopes)BuilderApply scope array to query
applyAggregates($query, $aggregates)BuilderApply aggregate functions
applySearch($query)voidApply ?search= if model is Searchable
paginateQuery($query)mixedPaginate based on $paginationType
assertFillableColumn($model, $column)voidVerify column exists and is fillable
registerPermissionMiddleware()voidRegister Spatie permission middleware
permissionMiddleware($slug)array<Middleware>Static — generate middleware array

Released under the MIT License.