Laravel Toolkit is a curated set of reusable components aimed at improving productivity and consistency across Oltrematica’s Laravel projects.
This package provides utilities, helpers, macros, traits, and value objects commonly used in our codebase, offering ready-made solutions for frequent challenges and promoting shared best practices. Laravel Toolkit was created to centralize cross-cutting or repetitive code, reducing duplication and making maintenance easier.
Features include standardized operation result handling (Result pattern), string, array, and date helpers, Eloquent and Collection macros, reusable traits, and other tools for everyday development.
The package is designed to be lightweight, modular, and easily extensible, adapting to the evolving needs of Oltrematica’s teams.
- Laravel v10, v11 and v12
- PHP 8.3 or higher
composer require oltrematica/laravel-toolkitThe Result class is a simple, immutable value object designed to represent the outcome of an operation in a consistent way. It allows you to standardize how you return success, error, or failure (with exception) from your services, actions, or domain logic.
Use Result to make your service and action return values explicit and consistent, improving error handling and readability throughout your codebase.
use Oltrematica\Toolkit\Support\Result;
$result = Result::success('Operation completed successfully', ['foo' => 'bar']);
// Check if successful
if ($result->isSuccess()) {
$data = $result->getData();
// Handle success
}$result = Result::error('Validation failed', ['field' => 'email']);
// Check if error
if ($result->isError()) {
$message = $result->getMessage();
$data = $result->getData();
// Handle error
}try {
// Some operation that may throw
} catch (Throwable $e) {
$result = Result::failure($e, 'Unexpected exception occurred');
}
// Check if failure (error with exception)
if ($result->isFailure()) {
$exception = $result->getThrowable();
// Handle failure, log exception, etc.
}| Method | Description |
|---|---|
| Result::success() | Create a success result (optionally with message/data) |
| Result::error() | Create an error result (optionally with message/data/exception) |
| Result::failure() | Create an error result with an exception |
| isSuccess() | Returns true if result is successful |
| isError() | Returns true if result is an error (including failures) |
| isFailure() | Returns true if result is an error with exception |
| getMessage() | Returns the result message |
| getData() | Returns the attached data |
| getThrowable() | Returns the exception, if any |
The ResponseStatus enum defines a standard set of response statuses to represent the outcome of operations.
use Oltrematica\Toolkit\Enums\ResponseStatus;
$status = ResponseStatus::SUCCESS;
if ($status->isSuccess()) {
// Handle success
} elseif ($status->isError()) {
// Handle error
} elseif ($status->isFailure()) {
// Handle failure
}The project includes automated tests and tools for code quality control.
Rector is a tool for automating code refactoring and migrations. It can be run using the following command:
composer refactorPhpStan is a tool for static analysis of PHP code. It can be run using the following command:
composer analysePint is a tool for formatting PHP code. It can be run using the following command:
composer formatThe project includes automated tests and tools for code quality control.
composer testFeel free to contribute to this package by submitting issues or pull requests. We welcome any improvements or bug fixes you may have.