# Database design (ERD) and metadata

![](./erd.svg)

--------------------------------------------------------------------------------

# Tables

## tbl_addresses

```php
Schema::create('tbl_brands', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | ---------------------
id          | id of brands
name        | name of brands
description | description of brands

## tbl_brands

```php
Schema::create('tbl_brands', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | ---------------------
id          | id of brands
name        | name of brands
description | description of brands

## tbl_categories

```php
Schema::create('tbl_categories', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | ---------------------
id          | id of brands
name        | name of brands
description | description of brands

## tbl_items

```php
Schema::create('tbl_items', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | ---------------------
id          | id of brands
name        | name of brands
description | description of brands

## tbl_orders

```php
Schema::create('tbl_orders', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

  $table->string('status')->default('pending');
});
```

field       | description
----------- | ---------------------
id          | id of orders
name        | name of orders
description | description of orders
status      | status of orders

## tbl_projects

```php
Schema::create('tbl_projects', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | -----------------------
id          | id of projects
name        | name of projects
description | description of projects

## tbl_user_groups

```php
Schema::create('tbl_user_groups', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | --------------------------
id          | id of user_groups
name        | name of user_groups
description | description of user_groups

## tbl_user_roles

```php
Schema::create('tbl_user_roles', function (Blueprint $table) {
  $table->id();
  $table->timestamps();
  $table->string('name')->nullable();
  $table->text('description')->nullable();

});
```

field       | description
----------- | -------------------------
id          | id of user_roles
name        | name of user_roles
description | description of user_roles

## users

```php
Schema::create('users', function (Blueprint $table) {
  $table->id();
  $table->string('username');
  $table->string('firstname')->nullable();
  $table->string('lastname')->nullable();
  $table->string('email')->unique();
  $table->timestamp('email_verified_at')->nullable();
  $table->string('password');
  $table->string('address')->nullable();
  $table->string('city')->nullable();
  $table->string('country')->nullable();
  $table->string('postal')->nullable();
  $table->integer('status')->default(1);
  $table->integer('groups')->default();
  $table->integer('roles')->default(5);
  $table->text('about')->nullable();
  $table->rememberToken();
  $table->timestamps();
});
```

field             | description
----------------- | -------------------------------------------------------------
username          | username of user
firstname         | first name of user
lastname          | last name of user
email             | email of user
email_verified_at | email verification time of user
password          | salted passdword of usera
address           | address of user
city              | city of user
country           | country of user
postal            | postal of user
status            | status of user
groups            | groups of user
roles             | roles of user, default to `Guest` (ref: to `tbl_user_groups`)
about             | about of user

# relation tables (many-to-many table)

## rel_address_item.php

```php
Schema::create('rel_address_item', function (Blueprint $table) {
  $table->id();

  $table->unsignedBigInteger('item_id');
  $table
    ->foreign('item_id')
    ->references('id')
    ->on('tbl_items')
    ->onDelete('cascade');

  $table->unsignedBigInteger('address_id');
  $table
    ->foreign('address_id')
    ->references('id')
    ->on('tbl_addresses')
    ->onDelete('cascade');

  $table->timestamps();
});
```

field      | description
---------- | ------------------------------------
item_id    | id of items (ref: tbl_items->id)
address_id | id of items (ref: tbl_addresses->id)

## rel_brand_item.php

```php
Schema::create('rel_brand_item', function (Blueprint $table) {
  $table->id();

  $table->unsignedBigInteger('item_id');
  $table
    ->foreign('item_id')
    ->references('id')
    ->on('tbl_items')
    ->onDelete('cascade');

  $table->unsignedBigInteger('brand_id');
  $table
    ->foreign('brand_id')
    ->references('id')
    ->on('tbl_brands')
    ->onDelete('cascade');

  $table->timestamps();
});
```

field    | description
-------- | ----------------------------------
item_id  | id of items (ref: tbl_items->id)
brand_id | id of brands (ref: tbl_brands->id)

## rel_category_item.php

```php
Schema::create('rel_category_item', function (Blueprint $table) {
  $table->id();

  $table->unsignedBigInteger('item_id');
  $table
    ->foreign('item_id')
    ->references('id')
    ->on('tbl_items')
    ->onDelete('cascade');

  $table->unsignedBigInteger('category_id');
  $table
    ->foreign('category_id')
    ->references('id')
    ->on('tbl_categories')
    ->onDelete('cascade');

  $table->timestamps();
});
```

field         | description
------------- | ------------------------------------------
item_id       | id of items (ref: tbl_items->id)
categories_id | id of categories (ref: tbl_categories->id)

## rel_order_owner.php

```php
Schema::create('rel_order_owner', function (Blueprint $table) {
  $table->id();
  $table->timestamps();

  $table->unsignedBigInteger('order_id');
  $table
    ->foreign('order_id')
    ->references('id')
    ->on('tbl_orders')
    ->onDelete('cascade');

  $table->unsignedBigInteger('user_id');
  $table
    ->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade');

});
```

field    | description
-------- | ---------------------------------
order_id | id of items (ref: tbl_orders->id)
user_id  | id of categories (ref: users->id)

## rel_order_project.php

```php
Schema::create('rel_order_project', function (Blueprint $table) {
  $table->id();

  $table->unsignedBigInteger('project_id');
  $table
    ->foreign('project_id')
    ->references('id')
    ->on('tbl_projects')
    ->onDelete('cascade');

  $table->unsignedBigInteger('order_id');
  $table
    ->foreign('order_id')
    ->references('id')
    ->on('tbl_orders')
    ->onDelete('cascade');

  $table->timestamps();
});
```

field      | description
---------- | -------------------------------------
project_id | id of project (ref: tbl_projects->id)
order_id   | id of order (ref: tbl_orders->id)

## rel_project_owner.php

```php
Schema::create('rel_project_owner', function (Blueprint $table) {
  $table->id();
  $table->timestamps();

  $table->unsignedBigInteger('project_id');
  $table
    ->foreign('project_id')
    ->references('id')
    ->on('tbl_projects')
    ->onDelete('cascade');

  $table->unsignedBigInteger('user_id');
  $table
    ->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade');

});
```

field      | description
---------- | -------------------------------------
project_id | id of project (ref: tbl_projects->id)
order_id   | id of order (ref: tbl_orders->id)

## rel_user_item.php

```php
Schema::create('rel_user_item', function (Blueprint $table) {
  $table->id();

  $table->unsignedBigInteger('item_id');
  $table
    ->foreign('item_id')
    ->references('id')
    ->on('tbl_items')
    ->onDelete('cascade');

  $table->unsignedBigInteger('user_id');
  $table
    ->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade');

  $table->timestamps();
});
```

field   | description
------- | -------------------------------
item_id | id of item (ref: tbl_items->id)
user_id | id of user (ref: users->id)

## rel_user_user_group.php

```php
Schema::create('rel_user_user_group', function (Blueprint $table) {
  $table->id();
  $table->timestamps();

  // default to guest
  $table->unsignedBigInteger('user_group_id');
  $table
    ->foreign('user_group_id')
    ->references('id')
    ->on('tbl_user_groups')
    ->onDelete('cascade');

  $table->unsignedBigInteger('user_id');
  $table
    ->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade');

});
```

field         | description
------------- | -------------------------------------------
user_group_id | id of user_group (ref: tbl_user_groups->id)
user_id       | id of users (ref: users->id)
