Install Package

composer require codexshaper/laravel-database-manager

Setup for Mysql , Pgsql , Sqlite and Sqlsrv

Create database and setup .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-database-manager
DB_USERNAME=root
DB_PASSWORD=                    
php artisan dbm:install
Add Laravel\Passport\HasApiTokens Trait in your App\User Model

<?php 

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;
}
                                    
Next, Call the Passport::routes method within the boot method of your App\Providers\AuthServiceProvider.

<?php 

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}
                                    
Finally, in your config/auth.php configuration file, change the driver option of the api authentication guard token to passport.

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],
                                    

Setup for MongoDB

composer require codexshaper/laravel-database-manager
Add mongodb driver in your config/database.php

'mongodb' => [
    'driver'   => 'mongodb',
    'host'     => env('DB_HOST', 'localhost'),
    'port'     => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'password' => env('DB_PASSWORD'),
    'options'  => [
        'database' => 'admin', // sets the authentication database required by mongo 3
    ],
],
                                    
Create database and setup .env

DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=test
DB_USERNAME=
DB_PASSWORD=                      
composer require jenssegers/mongodb
php artisan dbm:install
Extends User model from CodexShaper\DBM\MongoDB\Auth\User and Add collection name protected $collection = 'custom_users'; in your User model if your users table different from users

<?php

namespace App;

use CodexShaper\DBM\MongoDB\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $collection = 'custom_users';
}
                                    
Next, you should call the Passport::routes method within the boot method of your App\Providers\AuthServiceProvider.

<?php 

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}
                                    
Go to config/auth.php configuration file, you should set the driver option of the api authentication guard to passport.

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],
                                    
Finally, in your config/dbm.php configuration file, you should change the auth.user.localkey = _id

'auth'                 => [
    "token" => [
        "expiry" => 24 * 60 * 60 * 1000, // 24 hours as a milliseconds
    ],
    'user'  => [
        'model'        => 'App\\User',
        'table'        => 'users',
        'local_key'    => '_id', // MongoDB
        // 'local_key'    => 'id', // Others
        'display_name' => 'name',
    ],
],
                                    

Create admin account to access all features

php artisan dbm:admin 'user' 'action' 'options'

Example

php artisan dbm:admin admin@admin.com create --column=email
In this case email must be exists in your users table and admin@admin.com must be a record

Build a table

Make CRUD

CRUD Object

CRUD Object

Label: Table Name

Tag: input

Type: hidden

Required: yes

Description: Table or Object name

Label: Create Model

Tag: input

Type: checkbox

Required: no

Default: false

Description: Enable create a new model if not exists

Label: URL Slug (must be unique)

Tag: input

Type: text

Required: yes

Description: Enter a unique slugify url

Label: Display Name (Plural)

Tag: input

Type: text

Required: yes

Description: Enter display name

Label: Model Name

Tag: input

Type: text

Required: yes

Description: Enter eloquent model with namespace

Example: App\\Test

Label: Controller Name

Tag: input

Type: text

Required: no

Default: null

Description: Enter the controller name

Example: App\\Http\\Controllers\\TestController

Label: Find Column

Tag: select

Type: dropdown

Required: yes

Description: Choose the local column field. It'll use when find or select a record from current object

Label: Search Column

Tag: select

Type: dropdown

Required: no

Default: all fields

Description: This column will use when

Label: Pagination

Tag: select

Type: dropdown

Required: no

Default: 15

Description: How many items show in per page?

Label: Order column

Tag: select

Type: dropdown

Required: yes

Description: Choose order column that will be use when order data.

Label: Order display column

Tag: select

Type: dropdown

Required: no

Default: null

Description: Order column display name

Label: Order direction

Tag: select

Type: dropdown

Required: yes

Default: Descending

Description: Choose order direction.

CRUD Fields

CRUD Fields

Label: Field

Description: Display value of the field

Label: Permissions

Type: Multiple Checkbox

Description: Field permissions for CRUD

Label: Type

Type: Dropdown

Description: Input type for each field

Label: Dispal Name

Type: text

Description: This option allow to set the display name for the filed when CRUD execute.

Label: Settings

Type: Json

Description: This option allow to set the field settings for input

1. Options:

Options for dropdown , multiple dropdown , checkbox , multiple checkbox , radio button must be an array where each element of array must be object with name and value pair

{
    "options": [
        {
            "name": "male",
            "value": "Male"
        },
        {
            "name": "female",
            "value": "Female"
        }
    ]
}
                                         
Set options for dropdown , multiple dropdown , checkbox , multiple checkbox , radio button using controller

{ 
   "options":{ 
      "controller":"App\\Http\\Controllers\\OptionController@index"
   }
}
                                         
In App\\Http\\Controllers\\OptionController return must be an array where each element of array must be object with name and value pair

<php

namespace App\Http\Controllers;

class OptionController extends Controller
{
    public function index()
    {
        $options = [];

        $options[] = (object) [
            'name'  => 'Option 1',
            'value' => 'option_1',
        ];

        $options[] = (object) [
            'name'  => 'Option 2',
            'value' => 'option_2',
        ];

        $options[] = (object) [
            'name'  => 'Option 3',
            'value' => 'option_3',
        ];

        return $options;
    }
}
                                         

2. Validation:

Field validation for both create and update together in one line

{
    "validation": "required"
}
                                         
Field validation for create and update separately as object

{
    "validation": {
        "create": {
            "rules": "required"
        },
        "update": {
            "rules": "required"
        }
    }
}
                                         
Add localKey for escape current record when validate unique field

// For all drivers except mongodb
{
    "validation": {
        "create": {
            "rules": "required|unique:users"
        },
        "update": {
            "rules": "required|unique:users,email",
            "localKey": "id"
        }
    }
}
// Only for mongodb
{
    "validation": {
        "create": {
            "rules": "required|unique:users"
        },
        "update": {
            "rules": "required|unique:users",
            "localKey": "email"
        }
    }
}
                                         

Relationship

Currently supports Has One , Has Many , Belongs To (Reverse HasOne and HasMany) and Belongs To Many (Many to Many)

Has One

Has One

Label: Relationship Type

Type: dropdown

Required: required

Description: Select what type relation you want

Label: Foreign Table

Type: dropdown

Required: required

Description: Select table (Where) you want to make relation has one?

Label: Foreign Table Model

Type: text

Required: required

Description: Full qualified class (class name with full namespace)

example:

App\\DbmField

Label: Local Column

Type: dropdown

Required: required

Description: Select column name from table (Which) you want to make relation has one?

Label: Foreign Column

Type: dropdown

Required: required

Description: Select column name from table (Where) you want to make relation has one?

Label: Display the {foreign table}

Type: dropdown

Required: required

Description: Select column name from table (Where) that will show as a lebel when make CRUD

Has Many

Has Many

Label: Relationship Type

Type: dropdown

Required: required

Description: Select what type relation you want

Label: Foreign Table

Type: dropdown

Required: required

Description: Select table (Where) you want to make relation has one?

Label: Foreign Table Model

Type: text

Required: required

Description: Full qualified class (class name with full namespace)

example:

App\\DbmField

Label: Local Column

Type: dropdown

Required: required

Description: Select column name from table (Which) you want to make relation has one?

Label: Foreign Column

Type: dropdown

Required: required

Description: Select column name from table (Where) you want to make relation has one?

Label: Display the {foreign table}

Type: dropdown

Required: required

Description: Select column name from table (Where) that will show as a lebel when make CRUD

Belongs To

Belongs To

Label: Relationship Type

Type: dropdown

Required: required

Description: Select what type relation you want

Label: Foreign Table

Type: dropdown

Required: required

Description: Select table (Where) you want to make relation has one?

Label: Foreign Table Model

Type: text

Required: required

Description: Full qualified class (class name with full namespace)

example:

App\\DbmField

Label: Local Column

Type: dropdown

Required: required

Description: Select column name from table (Which) you want to make relation has one?

Label: Foreign Column

Type: dropdown

Required: required

Description: Select column name from table (Where) you want to make relation has one?

Label: Display the {foreign table}

Type: dropdown

Required: required

Description: Select column name from table (Where) that will show as a lebel when make CRUD

Many To Many

Many to Many

Label: Relationship Type

Type: dropdown

Required: required

Description: Select what type relation you want

Label: Foreign Table

Type: dropdown

Required: required

Description: Select table (Where) you want to make relation has one?

Label: Foreign Table Model

Type: text

Required: required

Description: Full qualified class (class name with full namespace)

example:

App\\Role

Label: Pivot Table

Type: dropdown

Required: required

Description: Select interm table that will hold relationship tables information

Label: Parent Pivot Key

Type: dropdown

Required: required

Description: Select column name from pivot table (from)

Example:

Suppose you want to build relation between users and roles from users to roles where pivot/interm table is role_user which has two columns user_id that represents users table and another one is role_id that represents roles table. In this case Parent Pivot Key is user_id

Label: Related Pivot Key

Type: dropdown

Required: required

Description: Select column name from pivot table (to)

Example:

Suppose you want to build relation between users and roles from users to roles where pivot/interm table is role_user which has two columns user_id that represents users table and another one is role_id that represents roles table. In this case Parent Pivot Key is role_id

Label: Display the {foreign table}

Type: dropdown

Required: required

Description: Select column name from table (Where) that will show as a lebel when make CRUD

Backup

To backup your database your device must be install desire driver dump software. click here for more details. If you want to modify backup settings go to config/dbm.php then change your backup settings

'backup'               => [
    // Mysql
    'mysql'                => [
        'binary_path' => "", // c:\\xampp\\mysql\\bin\\
    ],
    // Sqlite 3
    'sqlite'               => [
        'binary_path' => "", // C:\\sqlite3\\
    ],
    // Postgree Sql
    'pgsql'                => [
        'binary_path' => "", // C:\\pgsql\\bin\\
    ],
    // MongoDB
    'mongodb'              => [
        'binary_path' => "", // C:\\Program Files\\MongoDB\\Server\\4.0\bin\\
        // "dsn" => "mongodb+srv://maab:Abuahsan91@laravel-mongodb-t5jhc.mongodb.net/laravel-database-manager",
    ],
    // Backup Directry in /storage/app/
    'dir'                  => 'backups',
    // Enable compression. By default true
    'compress'             => true,
    // Set compressor binary path to execute compression
    'compress_binary_path' => "",
    // Set compressor extension
    'compress_extension'   => ".gz",
    // Set compress command
    'compress_command'     => "gzip",
    // Set uncompress command
    'uncompress_command'   => "gunzip",
    // Enable debug when developemnt mode. By default false
    'debug'                => false,
],
                                 

Server Configuration for Database Backup

When you run your project in sub directory or sub domain then may you need to configure your server to work database dumper. Sometime may dumper (specially mysql) not working because of misconfiured. Here I show you how to configure your laravel project in xampp on windows 10.

Step 1: Open your hosts file. In my case C:\Windows\System32\drivers\etc\hosts and add below line at end of file

127.0.0.1 yourdomain.com

Save the file.

Step 2: Setup virtual host to link your domain with your project. In my case C:\xampp\apache\conf\extra\httpd-vhosts.conf



<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot C:\xampp\htdocs\project-folder\public
 
    SetEnv APPLICATION_ENV "development"
 
    <Directory C:\xampp\htdocs\project-folder\public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
                          

Change yourdomain.com to your original domain and project-folder to your own project folder.

Finaly, Restart your server. Done!

Config

To edit your own configuration go to config/dbm.php then change your settings

Base Path

When your root directory not public then specify the public directory. EX: your public directory is http://localhost/laravel-database-manager/public then simply add /laravel-database-manager/public in base_path

/*
|-------------------------------------------------------------
| Base Path
|-------------------------------------------------------------
|
| Base URL
| by default root path. You can add base path /path/to/project/public
|
| Don't put trailing/end slash(/)
|
|
 */

'base_path'               => '',
                              

Prefix

Set URL prefix after base path. Default value prefix is /database

/*
|-------------------------------------------------------------
| Prefix
|-------------------------------------------------------------
|
| Set custom URL prefix
| by default /database . You can add prefix before /database
| Currently supports only empty string. You can use it later
|
| Don't put trailing/end slash(/)
|
|
 */

'prefix'               => '/database',
                              

Publish

Publish resources

Publish Config

php artisan vendor:publish --tag=dbm.config

Publish Migrations

php artisan vendor:publish --tag=dbm.migrations

Publish Seeds

php artisan vendor:publish --tag=dbm.seeds

Publish Views

php artisan vendor:publish --tag=dbm.views

Publish full resource

php artisan vendor:publish --tag=dbm.resources

Commands

Install

php artisan dbm:install

Seed Migrations

php artisan dbm:seed

Make databse admin

php artisan dbm:admin {findValue}
OR specify column name
php artisan dbm:admin {findValue} create --column={findColumn}

Remove databse admin

php artisan dbm:admin {findValue} drop
OR specify column name
php artisan dbm:admin {findValue} drop create --column={findColumn}

Make Full database Backup

php artisan dbm:backup

Make single table Backup

php artisan dbm:backup --t=tableName
OR
php artisan dbm:backup --table=tableName

Restore Database using full path

php artisan dbm:restore --p=fullPath
OR
php artisan dbm:restore --path=fullPath

Restore Database using filename

php artisan dbm:restore --f=fileName
OR
php artisan dbm:restore --file=fileName