whilesmart / laravel-configurations
Laravel package for managing model-specific configurations with type safety and easy persistence
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/whilesmart/laravel-configurations
Requires
- laravel/sanctum: ^4.1
Requires (Dev)
- fakerphp/faker: ^1.24
- laravel/pint: ^1.22
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.4
- zircote/swagger-php: ^5.1
- dev-dev
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-main
- dev-release-1.0.4
- dev-docs/release-notes
- dev-fix/middleware
- dev-release-1.0.3
- dev-docs/api-documentation
- dev-release-1.0.2
- dev-release-1.0.1
- dev-chore/add-release-script
- dev-chore/rename-package
- dev-refactor/namespace-as-model-configurations
- dev-release-1.0.0
- dev-develop
This package is auto-updated.
Last update: 2025-10-24 08:54:42 UTC
README
This Laravel package provides a complete configuration solution ready to be integrated into your application.
Features
- NoSQL-like Flexibility: Add schema-less dynamic attributes to any Eloquent model without database migrations
- Type-Safe Storage: Supports strings, integers, arrays, JSON, dates with automatic type casting via
ConfigValueTypeenum - Polymorphic Relationships: Any model can have configurations through a single, flexible relationship
- Ready-to-use endpoints: Complete API for managing configurations
- OpenAPI documentation: Automatically generated documentation using PHP attributes
- Configuration file: Easily customize settings
- Laravel agnostic considerations: designed with future framework agnosticism in mind
Development
This package includes a Docker development environment and Makefile for easy development.
Quick Start
# Start the development environment make up # Install dependencies make install # Run tests make test # Run code formatter make pint # Show all available commands make help
Available Make Commands
make up- Start Docker containersmake down- Stop Docker containersmake install- Install dependenciesmake test- Run testsmake pint- Run Laravel Pint code formattermake lint- Alias for pintmake fresh- Fresh start (down, up, install)make setup- Complete setup with testsmake check- Run all checks (formatting + tests)make shell- Access container shell
Installation
1. Require the package
composer require whilesmart/eloquent-model-configuration
2. Publish the configuration and migrations:
You do not need to publish the migrations and configurations except if you want to make modifications. You can choose to publish the migrations, routes, controllers separately or all at once.
2.1 Publishing only the routes
Run the command below to publish only the routes.
php artisan vendor:publish --tag=model-configuration-routes php artisan migrate
The routes will be available at routes/model-configuration.php. You should require this file in your api.php file.
require 'model-configuration.php';
2.2 Publishing only the migrations
+If you would like to make changes to the migration files, run the command below to publish only the migrations.
php artisan vendor:publish --tag=model-configuration-migrations php artisan migrate
The migrations will be available in the database/migrations folder.
2.3 Publish only the controllers
By default the controllers assign the device to the currently logged in user. If you would like to assign the device to
another model, you can publish the controllers and make the necessary changes to the published file.
To publish the controllers, run the command below
php artisan vendor:publish --tag=model-configuration-controllers php artisan migrate
The controllers will be available in the app/Http/Controllers directory.
Finally, change the namespace in the published controllers to your namespace.
2.4 Publish the documentation
To publish the openapi documentation, run the command below
php artisan vendor:publish --tag=model-configuration-docs
The documentation will be available at app/Http/Documentation
2.5 Publish everything
To publish the migrations, routes and controllers, you can run the command below
php artisan vendor:publish --tag=model-configuration php artisan migrate
Note: See section 2.1 above to make the routes accessible
2.6 Middleware configuration
The default controller gets the authenticated user from the Request object. The default routes must be placed in an auth middleware. Set your auth middleware in the config/model-configuration.php file.
<?php return [ ..., 'auth_middleware' => ['auth:sanctum'], ];
2.7 More configurations
Config keys are case-insensitive by default. For example ConfigName and configname are considered the same. To change this behavior, set the allow_case_insensitive_keys config variable to true.
<?php return [ ..., 'allow_case_insensitive_keys' => true, ];
Config names can take only the following:
- Lowercase characters: a to z
- Uppercase characters: A to Z
- Numbers: 0 to 9
- Some special characters: hyphen (-), underscore (_), fullstop (.) and plus (+)
3. Model Relationships
We have implemented a Trait Configurable that handles relationships. If your model has configuration, simply use the
Configurable trait in your model definition.
use Whilesmart\ModelConfiguration\Traits\Configurable class MyModel { use Configurable; }
You can call yourModel->configurations() to get the list of configuration tied to the model
$model = new MyModel(); $model->configurations();
The Configurable trait also has the getConfigurationssAttribute() method. If you want to append the configuration to the model response, simply add configuration to your model's $appends
use Whilesmart\ModelConfiguration\Traits\Configurable; class MyModel { use Configurable; protected $appends = ['configurations']; }
The following additional methods are available also available in the Configurable trait
getConfig()gets the full details of a particular config. It returns aConfigurationobject
$model = new MyModel(); $config = $model->getConfig('default_wallet'); $config_id = $config->id;
getConfigValue()gets the value of a particular config. It also ensures the value is returned in the correct type
$model = new MyModel(); $config_value = $model->getConfigValue('default_wallet');
getConfigType()gets the type of a particular config. It returns aConfigValueTypeenum
$model = new MyModel(); $config_type = $model->getConfigType('default_wallet');
setConfigValue()sets/updates the value of a particular config. It returns aConfigurationobject
$model = new MyModel(); $config= $model->setConfigValue('default_wallet',1, ConfigValueType::Integer);
Usage
After installation, the following API endpoints will be available:
- POST /api/configurations: Registers a new config linked to the current logged in user.
- GET /api/configurations: Retrieves all configurations linked to the current logged in user.
- PUT /api/configurations/{id}: Updates the information in a configuration.
- DELETE /api/configurations/{id}: Deletes a configuration.
- OpenAPI Documentation: Accessible via a route that your OpenAPI package defines.
Example Request:
{
"value":"unique_token_string",
"type":"string"
}