PHPackages                             owowagency/laravel-settings - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. owowagency/laravel-settings

ActiveLibrary

owowagency/laravel-settings
===========================

A package of settings features for the Laravel framework.

v1.0.0(2y ago)07421[2 issues](https://github.com/owowagency/laravel-settings/issues)[1 PRs](https://github.com/owowagency/laravel-settings/pulls)PHPPHP ^8.1

Since Nov 2Pushed 2y agoCompare

[ Source](https://github.com/owowagency/laravel-settings)[ Packagist](https://packagist.org/packages/owowagency/laravel-settings)[ RSS](/packages/owowagency-laravel-settings/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (6)Versions (4)Used By (0)

Laravel Settings
================

[](#laravel-settings)

[![Total Downloads](https://camo.githubusercontent.com/8848df6d377d634a87ccba5ec83710d3b8ead40802e1c08bbc6758284430c030/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f776f776167656e63792f6c61726176656c2d73657474696e6773)](https://packagist.org/packages/owowagency/laravel-settings)[![Latest Stable Version](https://camo.githubusercontent.com/030b86e2ff915ca7359896c0603763d03cd2559757afca71a6dd3cd0892d9809/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f776f776167656e63792f6c61726176656c2d73657474696e6773)](https://packagist.org/packages/owowagency/laravel-settings)

A package of settings features for the Laravel framework.

Installation
============

[](#installation)

Installing this package can be done easily via the following Artisan command.

```
composer require owowagency/laravel-settings
```

Setup
=====

[](#setup)

To install all the vendor files you can run the following command.

```
php artisan vendor:publish --provider="OwowAgency\LaravelSettings\LaravelSettingsServiceProvider"
```

This will copy all the vendor files, including configuration, migrations and resources. If you wish to only install certain files you can use the command described in the next paragraphs.

Config
------

[](#config)

If you wish to publish the configuration file, you can use the following command:

```
php artisan vendor:publish --provider="OwowAgency\LaravelSettings\LaravelSettingsServiceProvider" --tag=config
```

### Settings

[](#settings)

The `settings` configuration holds all the values which can be used within the application. Each setting should have a unique key. For setting the key we would recommend using a package like [Laravel Enum](https://github.com/BenSampo/laravel-enum). Beside the key, we use the following attributes:

- `title` (default: `null`): here you can store a small title of the setting.
- `description` (default: `null`): a description about the setting which you might want to display to the user.
- `type` (default: `string`): the variable type of the setting (the type should be acceptable by the [`settype()`](https://www.php.net/manual/en/function.settype.php#refsect1-function.settype-description) method).
- `default` (default: `null`): the default value which will be used if the user hasn't stored the value in the database yet.
- `nullable` (default: `true`): indicates if this setting may have the `null` value.

### Table

[](#table)

The `table_name` configuration holds the value which is being used for the table name. By default, this has been set to `settings`, but if you wish to use a different table name you can change it with this configuration value.

### Resources

[](#resources)

The package uses Laravel its [API Resources](https://laravel.com/docs/8.x/eloquent-resources#generating-resources). We ship the package with one resource, which will return the setting model. If you wish to use a custom resource, you can specify them in this list.

Migrations
----------

[](#migrations)

If you wish to publish the migrations, you can use the following command:

```
php artisan vendor:publish --provider="OwowAgency\LaravelSettings\LaravelSettingsServiceProvider" --tag=migrations
```

Routes
------

[](#routes)

To set up all the routes needed for this package you can call the setting macro on the Route facade. By doing so, all the routes which are required by this package will be available to call.

```
Route::settings('users', App\Models\User::class);
```

After adding this to one of your routes files (i.e. `routes/api.php`), the following two routes will be available.

### Index

[](#index)

This route can be used to index all the settings of a user: `GET /users/{user}/settings`. This route will always return all configured settings in the `settings` config value. Also, values which are not yet stored for the authenticated user. The package will then use the default configured value.

**Response**

```
[
    {
        "title": "Receive commercial emails",
        "description": "Would you like to receive commercial emails for our marketing campaign?",
        "type": "bool",
        "default": false,
        "nullable": false,
        "key": "wants_promotion_emails",
        "value": false
    }
]
```

### Update

[](#update)

This route can be used to update the given setting values: `PATCH /users/{user}/settings`.

**Request**

```
[
    {
        "key": "wants_promotion_emails",
        "value": true
    }
]
```

**Response**

```
[
    {
        "title": "Receive commercial emails",
        "description": "Would you like to receive commercial emails for our marketing campaign?",
        "type": "bool",
        "default": false,
        "nullable": false,
        "key": "wants_promotion_emails",
        "value": true
    }
]
```

Usage
=====

[](#usage)

If you want a certain model (we'll use the user model in this example) to have settings you can add the `OwowAgency\LaravelSettings\Models\Concerns\HasSettings` trait to the model. First you should add the `OwowAgency\LaravelSettings\Models\Concerns\HasSettingsInterface` to the model. Your model could look like this.

```
