PHP code example of dev-toolbelt / laravel-fast-crud
1. Go to this page and download the library: Download dev-toolbelt/laravel-fast-crud library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dev-toolbelt / laravel-fast-crud example snippets
namespace App\Http\Controllers;
use App\Models\Product;
use DevToolbelt\LaravelFastCrud\CrudController;
class ProductController extends CrudController
{
protected function modelClassName(): string
{
return Product::query()->class;
}
}
use App\Http\Controllers\ProductController;
use DevToolbelt\LaravelFastCrud\Router;
Router::crud('products', ProductController::class, 'products');
class ProductController extends CrudController
{
// Query key used in filter[...] (default: term)
protected string $termFieldName = 'term';
// Fields used in the generated where/orWhere block
protected array $termFields = ['login', 'name', 'email'];
}
class ProductController extends CrudController
{
protected function modelClassName(): string
{
return Product::query()->class;
}
// Add data before filling the model
protected function beforeCreateFill(array &$data): void
{
$data['created_by'] = auth()->id();
}
// Modify data before saving
protected function beforeCreate(array &$data): void
{
$data['slug'] = Str::slug($data['name']);
}
// Dispatch events after creation
protected function afterCreate(Model $record): void
{
event(new ProductCreated($record));
}
// Add eager loading and scopes to search
protected function modifySearchQuery(Builder $query): void
{
$query->with(['category', 'brand'])
->where('is_active', true);
}
// Modify filters before they are applied to the query
protected function modifyFilters(array $filters): array
{
// Force a filter regardless of what the client sends
$filters['is_visible'] = ['eq' => true];
// Remove a filter that should not be used by the client
unset($filters['internal_code']);
return $filters;
}
// Provide user ID for soft delete audit
protected function getSoftDeleteUserId(): ?int
{
return auth()->id();
}
// Define validation rules for create
protected function createValidateRules(): array
{
return [
'name' => ['
// config/devToolbelt/fast-crud.php
use DevToolbelt\Enums\Http\HttpStatusCode;
return [
// Global settings (applied to all actions)
'global' => [
'find_field' => 'id', // Field to find records by
'find_field_is_uuid' => false, // Validate as UUID before querying
'validation' => [
'http_status' => HttpStatusCode::BAD_REQUEST->value, // 400
],
],
// Per-action settings (override global)
'create' => [
'method' => 'toArray', // Model method called to serialize the record in the response
'http_status' => HttpStatusCode::CREATED->value, // 201
],
'read' => [
'method' => 'toArray', // Model method called to serialize the record in the response
'http_status' => HttpStatusCode::OK->value, // 200
],
'update' => [
'method' => 'toArray', // Model method called to serialize the record in the response
'http_status' => HttpStatusCode::OK->value, // 200
],
'delete' => [
'http_status' => HttpStatusCode::OK->value, // 200
],
'soft_delete' => [
'deleted_at_field' => 'deleted_at',
'deleted_by_field' => 'deleted_by',
'http_status' => HttpStatusCode::OK->value, // 200
],
'restore' => [
'method' => 'toArray', // Model method called to serialize the record in the response
'http_status' => HttpStatusCode::OK->value, // 200
],
'search' => [
'method' => 'toArray', // Model method called to serialize each record in the response
'per_page' => 40,
'http_status' => HttpStatusCode::OK->value, // 200
],
'options' => [
'default_value' => 'id',
'http_status' => HttpStatusCode::OK->value, // 200
],
'export_csv' => [
'method' => 'toArray', // Model method called to serialize each record before export
'http_status' => HttpStatusCode::OK->value, // 200
],
];
// Return 202 Accepted instead of 201 Created
'create' => [
'method' => 'toArray',
'http_status' => HttpStatusCode::ACCEPTED->value, // 202
],
// Return 204 No Content instead of 200 OK for delete
'delete' => [
'http_status' => HttpStatusCode::NO_CONTENT->value, // 204
],