Install the package:
composer require miladimos/laravel-toolkit
php artisan toolkit:install
This generates the default helpers.php file in app/Helpers/.
First Use Case:
RouteKeyNameUUID trait in a model to auto-generate UUIDs for route keys and database IDs:
use Miladimos\Toolkit\Traits\RouteKeyNameUUID;
class Post extends Model
{
use RouteKeyNameUUID;
}
ApiResponder for consistent JSON responses:
use Miladimos\Toolkit\Traits\ApiResponder;
class PostController extends Controller
{
use ApiResponder;
public function show(Post $post)
{
return $this->successResponse($post);
}
}
Where to Look First:
app/Helpers/helpers.php (auto-generated) for helper methods.php artisan make:helper to scaffold new helper files.Model Enhancements:
HasUUID and RouteKeyNameUUID for UUID-based models:
use Miladimos\Toolkit\Traits\{HasUUID, RouteKeyNameUUID};
class Comment extends Model
{
use HasUUID, RouteKeyNameUUID;
}
HasTimestamps to enforce created_at/updated_at:
use Miladimos\Toolkit\Traits\HasTimestamps;
class LogEntry extends Model
{
use HasTimestamps;
}
API Layer:
return response()->json() with ApiResponder:
return $this->errorResponse('Invalid data', 422, ['errors' => $validator->errors()]);
HasJWT in controllers for token logic:
use Miladimos\Toolkit\Traits\HasJWT;
class AuthController extends Controller
{
use HasJWT;
public function login(Request $request)
{
return $this->generateJWT($user);
}
}
Helper Methods:
helpers.php with reusable logic:
if (!function_exists('slugify')) {
function slugify(string $text): string
{
return Str::slug($text);
}
}
GetConstantsEnum for type-safe constants:
use Miladimos\Toolkit\Traits\GetConstantsEnum;
class UserRole
{
use GetConstantsEnum;
const ADMIN = 'admin';
const USER = 'user';
}
Relationships:
HasTags:
use Miladimos\Toolkit\Traits\HasTags;
class Article extends Model
{
use HasTags;
}
HasComment:
use Miladimos\Toolkit\Traits\HasComment;
class BlogPost extends Model
{
use HasComment;
}
Integration Tips:
config/autoload.php:
'files' => [
app_path('Helpers/helpers.php'),
],
$mock = $this->getMockForTrait(ApiResponder::class);
Trait Conflicts:
boot() in HasUUID vs. HasTimestamps).protected overrides if needed:
protected static function bootHasUUID()
{
// Custom logic
}
UUID Generation:
RouteKeyNameUUID may clash with Laravel’s default incrementing behavior.$incrementing = false and $keyType = 'string' in your model.Helper Autoloading:
composer dump-autoload or update composer.json manually.JWT Traits:
HasJWT assumes tymon/jwt-auth is installed.composer require tymon/jwt-auth
Database Migrations:
HasUUID or HasTimestamps may not auto-create columns.boot() method to create migrations dynamically.Trait Methods Not Loading:
used in the correct class.HasJWT vs. HasJwt).API Response Issues:
ApiResponder uses response()->json(). Ensure your Laravel version supports it (v5.5+).successResponse() or errorResponse() for custom logic:
public function successResponse($data, int $status = 200, array $headers = [])
{
return response()->json(['data' => $data], $status, $headers);
}
Enum Constants:
GetConstantsEnum requires PHP 7.1+. For older versions, manually define constants.Organize Helpers:
helpers.php into modular files (e.g., StringHelpers.php, ArrayHelpers.php) and autoload them:
'files': [
app_path('Helpers/StringHelpers.php'),
app_path('Helpers/ArrayHelpers.php'),
],
Extend Traits:
namespace App\Traits;
use Miladimos\Toolkit\Traits\HasUUID;
trait CustomUUID extends HasUUID
{
public function generateCustomUUID()
{
return Str::uuid()->toString();
}
}
Performance:
Testing:
$model = new class extends Model {
use HasUUID;
};
$this->assertNotNull($model->uuid);
Configuration:
ApiResponder:
'api_responder' => [
'default_status' => 200,
'wrap_data' => true,
],
config/toolkit.php).Localization:
README-fa.md). Use php artisan lang:publish to customize translations if needed.Backward Compatibility:
ApiResponder) may need adjustments for older versions.
How can I help you explore Laravel packages today?