medalink / laravel-reflects-constants
A small helper package that aids in the retrieval of class constants of models in laravel.
Installs: 3 610
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.2.5|^8.0.2
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
README
A small helper package that aids in the retrieval of class constants of models.
composer require medalink/laravel-reflects-constants
How to use
Let's assume you have a class with defined constants for different types.
<?php class ProductInformation { use \Medalink\Reflects\Constants; /** * Optional constant blacklist, anything in here will be filtered */ public $reflectedConstantsBlacklist = [ 'TEST', ]; const TYPE_OVERVIEW = 'OVERVIEW'; const TYPE_SAFETY = 'SAFETY'; const TYPE_WARRANTY = 'WARRANTY'; const TYPE_PRODUCT_INFO = 'PRODUCT_INFO'; const TEST = 'TEST'; }
This package will allow you to return these constants as a human readable array. This is most useful when using this data in various places through your application. Let's take a look at using this package to help a factory choose a random type.
$factory->define(ProductInformation::class, function (Faker $faker) { return [ 'type' => $faker->randomElement(ProductInformation::getReflectedConstants('TYPE_')), ]; });
The resulting array would look like this:
$types = ProductInformation::getReflectedConstants('TYPE_'); $types = [ 'Overview', 'Safety', 'Warranty', 'Product Info' ];
Let's take a look at using this to populate a laravel nova options dropdown.
Select::make('Type') ->options(ProductInformation::getReflectedConstants('TYPE_')) ->sortable(),
The getReflectedConstants
supports a blacklist, a prefix, returning the prefix with the constant name, and a human readable toggle. Take a look at the source for more details on how to use these features.