Provides Laravel with additional features to help during startup, such as dynamic validation messages and data sharing between middleware and controllers
trevormh/laravel-startup-helper is a Laravel package for provides laravel with additional features to help during startup, such as dynamic validation messages and data sharing between middleware and controllers.
It currently has 0 GitHub stars and 1 downloads on Packagist.
Install it with composer require trevormh/laravel-startup-helper.
Discover more Laravel packages by trevormh
or browse all Laravel packages to compare alternatives.
Last updated
This package helps to reduce repeated tasks during the startup of your laravel app all the way through validation by easily allowing data from your middleware and service providers to be shared with your controllers via a content store. Validation messages can also be dynamically created in your custom validators.
composer require trevormh/laravel-startup-helper
First import the package into any php file with:
use trevormh\LaravelStartupHelper\StartupFactory;
To access the instance:
$helper = StartupFactory::resolve();
To add content to the store pass an associative array.
$helper->addContent([
'id' => $id,
'product' => $product
]);
To retrieve from the store you can either pass in a single parameter, or no parameters at all to retrieve the entire store.
// retrieve a single property
$id = $helper->get('id');
// retrieve all content
$store = $helper->get();
If you have custom validators you can create dynamic validation messages from within the validator. To do this in your controller create a validator and pass it to the startup helper
$validator = Validator::make($request->all(),[
'id' => 'integer|SomeCustomValidator'
]);
$helper->addValidator($validator);
Next in the custom validator, SomeCustomValidator, you can add custom error messages to be passed to the laravel validator
Validator::extend('SomeCustomValidator', function($attribute, $value $parameters) {
$helper = StartupFactory::resolve();
$someOtherValue = Model::find('id',$value);
if ($value !== $someOtherValue->fieldName) {
$helper->addErrorMessage("id.some_custom_validator", "The id " . $value . " has failed to validate");
return false;
}
// also add the query we just ran
$helper->addContent['someOtherValue' => $someOtherValue]);
return true;
});
Back in your controller where you called the custom validator from, you can continue validation as you normally would and if there is a validation failure, your custom error message will be set without any additional configuration
if ($validator->fails()) {
$errors = $validator->errors(); // this will contain the error message set in `SomeCustomValidator`
return $errors;
}
You can also retrieve the data from the query that was executed in SomeCustomValidator
$someOtherValue = $helper->get('someOtherValue');