Skip to content

Package for storing laravel config details in database

License

Notifications You must be signed in to change notification settings

nzsakib/db-config

Repository files navigation

Very short description of the package

Latest Version on Packagist Build Status Quality Score Total Downloads

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.

Installation

You can install the package via composer:

composer require nzsakib/db-config

Usage

Facade or Implementation Class?

You can either use facade or direct implementation class to work.

// You can use following facade
\Nzsakib\DbConfig\Facades\CustomConfig::getCollection();
// Or you can use the implementation below 
(new \Nzsakib\DbConfig\DbConfig())->getCollection();

Get All Configurations as collection from DB

use Nzsakib\DbConfig\DbConfig;

$config = new DbConfig;
$allConfig = $config->getCollection(); // returns Model collection of specified table

// pass to blade or do your thing by looping 
foreach($allConfig as $config) {
    dump($config->name);
    dump($config->value);
}

Set a new config

use Nzsakib\DbConfig\DbConfig;

$config = new DbConfig; 
$name = 'facebook';
$value = [
    'client_id' => 'a client id',
    'client_secret' => 'client secret',
];
// Value could be any data type e.g. boolean/array/string/integer

try {
    $newConfig = $config->set($name, $value); 
    // new config is set and cache is invalidated 
} catch (\InvalidArgumentException $e) {
    // redirect with message $e->getMessage() 
}

Update existing DB config

Cache will be deleted automatically after successfull update.

use Nzsakib\DbConfig\DbConfig;
use Illuminate\Database\Eloquent\ModelNotFoundException;

$config = new DbConfig;

$name = 'facebook';
$newValue = [
    'client_id' => 'updated client id',
    'client_secret' => 'updated secret'
];

try {
    $updatedConfig = (new DbConfig)->updateByName($name, $newValue); 
    // Updated model is returned 
} catch (ModelNotFoundException $e) {
    // Specified name does not exists in database
}

// Or you could update by `id` which is primary key 
try {
    $updatedConfig = (new DbConfig)->updateById($id, $name, $newValue);
    // Updated model is returned 
} catch (ModelNotFoundException $e) {
    // Specified id does not exists in database
}

Delete a DB Config

Cache will be deleted automatically after successfull delete.

use Nzsakib\DbConfig\DbConfig;
use Illuminate\Database\Eloquent\ModelNotFoundException;

$name = 'facebook';
try {
    $deletedConfig = (new DbConfig)->deleteByName($name);
    // deleted successfully 
} catch (ModelNotFoundException $e) {
    // specified name does not exists in database 
}

// Or delete the config by primary key `id` 
$id = request('id'); 
try {
    $deletedConfig = (new DbConfig)->deleteById($id);
    // deleted successfully 
} catch (ModelNotFoundException $e) {
    // specified id does not exists in database 
}

Get Eloquent DB Query to Work With Data

use Nzsakib\DbConfig\DbConfig;

$query = (new DbConfig)->getQuery(); 
// Returns Builder instance to underlying config table Model
// You can run custom query on it 
$query->where('name', 'facebook')->delete();
// facebook config row is deleted from DB

Publish the package config and migration files

php artisan vendor:publish --provider="Nzsakib\DbConfig\DbConfigServiceProvider" --tag="config"
php artisan vendor:publish --provider="Nzsakib\DbConfig\DbConfigServiceProvider" --tag="migrations"

You can change table name of the migration file, but make sure you mention the updated table name in the config file.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email sukku.mia@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

About

Package for storing laravel config details in database

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published