BaseWebController
Anil\FastApiCrud\Http\Controllers\BaseWebController
Abstract controller for Blade/web endpoints. Uses AuthorizesRequests and HasCrudOperations traits.
Constructor
public function __construct(
string $model, // class-string<Model>
string $storeRequest, // class-string<FormRequest>
string $updateRequest, // class-string<FormRequest>
string $viewPrefix, // e.g. 'admin.posts'
string $routePrefix, // e.g. 'admin.posts'
string $resourceName, // e.g. 'post'
string $collectionName, // e.g. 'posts'
?string $resource = null, // Optional class-string<JsonResource>
)Properties
Inherits all properties from HasCrudOperations. Additionally:
| Property | Type | Description |
|---|---|---|
$resource | class-string<JsonResource>|null | Optional. For data transformation. |
$viewPrefix | string | View name prefix. 'admin.posts' resolves to admin.posts.index, etc. |
$routePrefix | string | Route name prefix for redirects. |
$resourceName | string | Variable name for single model in show/edit views. |
$collectionName | string | Variable name for collection in index view. |
Public Methods
index
public function index(): ViewReturns {viewPrefix}.index view with ${collectionName} variable containing paginated results.
create
public function create(): ViewReturns {viewPrefix}.create view. Override to pass extra data (categories, tags, etc.).
store
public function store(): RedirectResponseCreates resource, redirects to {routePrefix}.index with success flash.
show
public function show(int|string $id): ViewReturns {viewPrefix}.show view with ${resourceName} variable.
edit
public function edit(int|string $id): ViewReturns {viewPrefix}.edit view with ${resourceName} variable.
update
public function update(int|string $id): RedirectResponseUpdates resource, redirects to {routePrefix}.index with success flash.
destroy
public function destroy(int|string $id): RedirectResponseDeletes resource, redirects with success flash.
delete
public function delete(): RedirectResponseBulk deletes, redirects with success flash.
changeStatus
public function changeStatus(int|string $id, string $column = 'status'): RedirectResponseupdateColumn
public function updateColumn(int|string $id, string $column = 'status'): RedirectResponserestore
public function restore(int|string $id): RedirectResponserestoreAll
public function restoreAll(): RedirectResponsepermanentDelete
public function permanentDelete(int|string $id): RedirectResponseProtected Helpers
viewName
protected function viewName(string $suffix): stringReturns "{$viewPrefix}.{$suffix}". Example: $this->viewName('create') → 'admin.posts.create'.
redirectWithSuccess
protected function redirectWithSuccess(string $route, string $message, array $parameters = []): RedirectResponseRedirects to the named route with a success flash message (key from config).
redirectBackWithError
protected function redirectBackWithError(string $message): RedirectResponseRedirects back with old input and an error flash message (key from config).
Overridable Message Methods
All return string. Override in your controller for localization:
| Method | Default Message |
|---|---|
storeSuccessMessage() | 'Record created successfully.' |
updateSuccessMessage() | 'Record updated successfully.' |
destroySuccessMessage() | 'Record deleted successfully.' |
bulkDeleteSuccessMessage() | 'Records deleted successfully.' |
statusChangeSuccessMessage() | 'Status updated successfully.' |
columnUpdateSuccessMessage() | 'Column updated successfully.' |
restoreSuccessMessage() | 'Record restored successfully.' |
restoreAllSuccessMessage() | 'All records restored successfully.' |
permanentDeleteSuccessMessage() | 'Record permanently deleted.' |