Installation:
composer require harryes/crudpackage
Publish the package config (if needed) with:
php artisan vendor:publish --provider="Harryes\CrudPackage\CrudPackageServiceProvider"
First Command:
Generate a basic CRUD structure for a User model with essential fields:
php artisan crud:generate User --columns=name:string,email:string,age:integer
This creates:
app/Models/User.php)database/migrations/..._create_users_table.php)app/Http/Controllers/UserController.php)app/Http/Resources/UserResource.php)routes/api.php)Where to Look First:
app/Http/Controllers/ for the controller logic.routes/api.php for API endpoints (e.g., /users, /users/{id}).app/Http/Resources/ for API response formatting.config/crudpackage.php for customization options.Rapid API Scaffolding:
php artisan crud:generate Product --columns=name:string,price:decimal,stock:integer,description:text
index, store, show, update, destroy methods in the controller.Integration with Existing Code:
// app/Http/Controllers/UserController.php
public function store(Request $request)
{
// Custom logic before saving
$validated = $this->validate($request);
$user = User::create($validated);
// Custom logic after saving
return (new UserResource($user))->response()->setStatusCode(201);
}
// app/Http/Resources/UserResource.php
public function toArray($request)
{
return array_merge(parent::toArray($request), [
'custom_field' => $this->whenLoaded('custom_field'),
]);
}
Dynamic Column Handling:
php artisan crud:generate Post --columns=title:string,content:text,is_published:boolean,published_at:timestamp
API Versioning:
routes/api.php:
Route::prefix('v1')->group(function () {
Route::apiResource('users', UserController::class);
});
Testing:
public function test_create_user()
{
$response = $this->postJson('/api/v1/users', [
'name' => 'Test User',
'email' => 'test@example.com',
'age' => 30
]);
$response->assertStatus(201);
}
Column Type Mismatches:
--columns parameter match Laravel’s migration syntax. For example:
timestamp instead of datetime for timestamps.decimal:8,2 for precise decimal values.Route Conflicts:
users and admins), Laravel may throw a RouteAlreadyRegistered error.Route::prefix('admin')->name('admin.')->apiResource('users', AdminUserController::class);
Soft Deletes:
softDeletes type adds deleted_at to the migration but doesn’t enable soft deletes in the model by default.use SoftDeletes; and $dates = ['deleted_at']; to the model.Validation Rules:
email:required|email are hardcoded.StoreUserRequest or UpdateUserRequest:
public function rules()
{
return array_merge(parent::rules(), [
'age' => 'nullable|integer|min:18',
]);
}
Foreign Key Constraints:
belongsTo, hasMany) automatically.// Model
public function posts()
{
return $this->hasMany(Post::class);
}
// Migration
$table->foreignId('user_id')->constrained()->onDelete('cascade');
Config Overrides:
config/crudpackage.php (published after installation).'generate_resource' => false,
Custom Templates:
php artisan vendor:publish --tag=crudpackage-views
Event Listeners:
created, deleted) in the controller or a service:
// app/Providers/EventServiceProvider.php
protected $listen = [
'Harryes\CrudPackage\Models\User' => [
'created' => [\App\Listeners\LogUserCreation::class],
],
];
API Middleware:
Route::middleware(['auth:sanctum'])->apiResource('users', UserController::class);
Testing Helpers:
// tests/Traits/CrudTestTrait.php
public function assertCrudOperations()
{
$this->assertRouteExists('users.index');
$this->assertRouteExists('users.store');
// Add more assertions...
}
Dynamic Permissions:
spatie/laravel-permission to restrict CRUD actions:
public function index()
{
$this->authorize('view', User::class);
return UserResource::collection(User::all());
}
How can I help you explore Laravel packages today?