BaseController
Anil\FastApiCrud\Http\Controllers\BaseController
Abstract controller for JSON API endpoints. Uses HasApiResponse, AuthorizesRequests, and HasCrudOperations traits.
Constructor
public function __construct(
string $model, // class-string<Model>
string $storeRequest, // class-string<FormRequest>
string $updateRequest, // class-string<FormRequest>
string $resource, // class-string<JsonResource>
)All parameters are validated. Throws Exception if any class doesn't extend the expected base class.
Properties
Inherits all properties from HasCrudOperations. Additionally:
| Property | Type | Description |
|---|---|---|
$resource | class-string<JsonResource> | Required. API resource class for response formatting. |
Public Methods
index
public function index(): AnonymousResourceCollectionLists all records with pagination, filtering, sorting, search, eager loads, counts, and aggregates.
show
public function show(int|string $id): JsonResourceReturns a single resource with eager loads, counts, aggregates, and scopes.
store
public function store(): JsonResponseCreates a new resource. Returns 201 Created. Validates via $storeRequest. Re-throws ValidationException for proper 422 responses.
update
public function update(int|string $id): JsonResource|JsonResponseUpdates the specified resource. Validates via $updateRequest.
destroy
public function destroy(int|string $id): JsonResponseDeletes a single resource (soft or force based on $forceDelete). Returns 204.
delete
public function delete(): JsonResponseBulk deletes records. Expects delete_rows array in request body. Returns 204.
changeStatus
public function changeStatus(int|string $id, string $column = 'status'): JsonResource|JsonResponseToggles a boolean column between 0 and 1.
updateColumn
public function updateColumn(int|string $id, string $column = 'status'): JsonResource|JsonResponseUpdates a specific fillable column with the request value.
restore
public function restore(int|string $id): JsonResource|JsonResponseRestores a single soft-deleted resource.
restoreAll
public function restoreAll(): JsonResponseRestores all soft-deleted resources. Returns 204.
permanentDelete
public function permanentDelete(int|string $id): JsonResponsePermanently deletes a soft-deleted resource. Returns 204.