whilesmart / laravel-model-configuration
Package for managing configurations across our projects
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
This package is not auto-updated.
Last update: 2025-08-24 06:42:33 UTC
README
This Laravel package provides a complete configuration solution ready to be integrated into your application.
Features
- Ready-to-use endpoints:
- 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/laravel-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 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
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 aConfiguration
object
$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 aConfigValueType
enum
$model = new MyModel(); $config_type = $model->getConfigType('default_wallet');
setConfigValue()
sets/updates the value of a particular config. It returns aConfiguration
object
$model = new MyModel(); $config= $model->setConfigValue('default_wallet',1, ConfigValueType::Integer);
Usage
After installation, the following API endpoints will be available:
- POST /configuration: Registers a new device linked to the current logged in user.
- Get /configuration: Retrieves all configuration linked to the current logged in user.
- PUT /configuration/{id}: Updates the device information.
- DELETE /configuration/{id}: Deletes a device from the database.
- OpenAPI Documentation: Accessible via a route that your OpenAPI package defines.
Example Request:
{ "value":"unique_token_string", "type":"string" }