ermradulsharma/laravel-toolkit
A lightweight Laravel toolkit bundling handy helpers and utilities for everyday development. Speed up common tasks, keep code clean, and add reusable tooling to new or existing Laravel apps.
Installation
composer require ermradulsharma/laravel-toolkit
Publish the config (if needed):
php artisan vendor:publish --provider="Ermradulsharma\Toolkit\ToolkitServiceProvider" --tag="config"
First Use Case: Artisan Commands The package includes utility commands. Run:
php artisan toolkit:list
This lists available toolkit commands (e.g., toolkit:make:model, toolkit:make:controller).
Key Features to Explore
Toolkit::success(), Toolkit::error()).Pattern: Extend Laravel’s generators with toolkit-specific defaults.
// Example: Custom model with timestamps disabled by default
php artisan toolkit:make:model Post -d --no-timestamps
Workflow:
toolkit:make:model instead of make:model for pre-configured traits (e.g., HasSoftDeletes).resources/views/vendor/toolkit/stubs/ to inject custom logic (e.g., API resource fields).Pattern: Replace manual return response()->json() with toolkit helpers.
use Ermradulsharma\Toolkit\Facades\Toolkit;
public function show(User $user) {
return Toolkit::success($user, 200, ['meta' => ['api_version' => 'v1']]);
}
Integration Tips:
config/toolkit.php (e.g., error codes, meta fields).Toolkit::setResponseWrapper(function ($response) {
return $response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
});
Pattern: Reuse validation rules across projects.
use Ermradulsharma\Toolkit\Rules\CustomRule;
$request->validate([
'email' => ['required', new CustomRule],
]);
Workflow:
app/Rules/ and register them in ToolkitServiceProvider.toolkit:make:rule to generate custom rules with built-in methods (e.g., passes(), message()).Pattern: Seed test data or mock responses efficiently.
// Seed factories
php artisan toolkit:factory:make UserFactory --count=10
// Mock API responses
Toolkit::mockHttpResponse('users/show', [
'id' => 1,
'name' => 'Test User',
]);
Tip: Combine with Laravel’s testing helpers for seamless integration:
$this->get('/api/users/1')->assertJsonToolkitSuccess();
Stub Overrides
resources/views/vendor/toolkit/stubs/ may conflict with package updates. Use php artisan toolkit:stub:publish to merge changes safely.Configuration Clashes
config/toolkit.php:
'api' => [
'default_status' => 201, // Override default success status
],
Command Naming Collisions
toolkit:* to prevent conflicts. Use namespaces:
php artisan make:command MyToolkitCommand
'debug' => env('TOOLKIT_DEBUG', false),
--verbose flag for detailed logs:
php artisan toolkit:make:model Post --verbose
Custom Commands
Extend the toolkit’s command registry in ToolkitServiceProvider:
public function registerCommands() {
$this->commands([
\App\Commands\CustomToolkitCommand::class,
]);
}
API Response Macros Add reusable response macros globally:
Toolkit::macro('customResponse', function ($data) {
return Toolkit::success($data, 200, ['custom_field' => true]);
});
Validation Rule Sharing Share rules across projects via a package:
composer require ermradulsharma/laravel-validation-rules
Then use them in any project:
use Ermradulsharma\ValidationRules\Rules\StrongPassword;
How can I help you explore Laravel packages today?