Installation:
composer require mohammedhassan/crud-package
The package auto-registers and installs wendelladriel/laravel-validated-dto as a dependency.
First Command:
Generate a basic CRUD scaffold for a Post resource:
php artisan make:crud Post
This creates:
Post.php) + MigrationPostController.php)PostService.php)CreatePostDto.php, UpdatePostDto.php)PostResource.php)routes/api.phpWhere to Look First:
database/schema/post_schema.php (auto-generated) for validation rules and column definitions.app/Dtos/Post/ (e.g., CreatePostDto.php).app/Services/PostService.php.app/Http/Controllers/PostController.php.Schema-Driven Development:
post_schema.php (e.g., title, body):
return [
'title' => ['type' => 'string', 'required' => true],
'body' => ['type' => 'text', 'required' => true],
];
php artisan make:crud Post --schema=database/schema/post_schema.php to regenerate files with updated rules.DTO Validation:
CreatePostDto):
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'body' => 'required|string',
];
}
public function store(CreatePostDto $dto)
{
$this->postService->create($dto);
}
Service Layer Integration:
// PostService.php
public function create(CreatePostDto $dto)
{
return Post::create($dto->toArray());
}
public function store(CreatePostDto $dto)
{
return $this->postService->create($dto);
}
API Resource Routing:
routes/api.php:
Route::apiResource('posts', PostController::class);
--api-route:
php artisan make:crud Post --api-route=routes/api/v2.php
Testing:
// Test DTO validation
$this->expectException(ValidationException::class);
CreatePostDto::fromArray([])->validate();
--force to overwrite files:
php artisan make:crud Post --force
resources/views/vendor/crud-package/ (if supported).PostCreated):
event(new PostCreated($post));
php artisan make:crud Post --api-route=routes/api/v1/posts.php
Schema File Path:
--schema is omitted, the package generates a default schema in database/schema/{name}_schema.php.php artisan make:crud Post --schema=database/schema/post_schema.php
DTO Validation Conflicts:
php artisan make:crud Post --schema=database/schema/post_schema.php --force
Route Name Collisions:
posts.index) may conflict with existing routes.--api-route to isolate routes or manually adjust Route::apiResource namespaces.Service Provider Binding:
AppServiceProvider if needed:
$this->app->bind(PostService::class, function ($app) {
return new CustomPostService();
});
Migration Conflicts:
make:crud after manual migrations may cause schema mismatches.--force:
php artisan make:crud Post --force
Command Output:
php artisan make:crud Post -v
DTO Validation Errors:
errors() method on the DTO instance:
$dto = CreatePostDto::fromArray($request->all());
if ($dto->fails()) {
return response()->json($dto->errors(), 422);
}
Service Layer Issues:
$service = new PostService();
$this->assertInstanceOf(Post::class, $service->create($dto));
Custom DTOs:
class CustomCreatePostDto extends CreatePostDto
{
public function additionalRules(): array
{
return ['slug' => 'required|unique:posts'];
}
}
Service Decorators:
class LoggedPostService extends PostService
{
public function create(CreatePostDto $dto)
{
Log::info('Creating post', $dto->toArray());
return parent::create($dto);
}
}
Schema Customization:
// In post_schema.php
return function () {
return [
'title' => ['type' => 'string', 'required' => request()->wantsJson()],
];
};
API Resource Extensions:
toArray() in PostResource:
public function toArray($request)
{
return array_merge(parent::toArray($request), [
'custom_field' => $this->resource->customAttribute,
]);
}
Command Customization:
php artisan vendor:publish --tag=crud-package-templates
config/crud-package.php for global defaults (e.g., namespace prefixes).How can I help you explore Laravel packages today?