【laralve項目】@11 商品數據庫設計-建立數據庫遷移文件

這一篇主要寫商品數據庫的設計,以及數據庫遷移文件的建立

在以前的文章中,咱們把前臺的登陸已經作完了,那麼做爲一個商城項目最重要的就是商品了,下來我們就對商品進行建立數據庫,以及數據遷移文件的使用php

商品數據庫的設計

思路:
咱們在建立商品表的時候須要先了解商品都有什麼屬性,這裏我直接就把已經設計好的模型放在這裏了,而後我們進行分析

1.咱們都知道咱們的商品是分主圖跟副圖之分的,咱們的主圖是現實在首頁或者分類頁面的數據,副圖是存在於商品詳情頁,因此咱們把副圖的數據新建一個表存放,而後在添加一個商品id的外鍵用來關聯商品,這就是咱們picture表存在的意義
2.咱們的商品除了在首頁顯示的,其他的應該都是在分類下,那麼咱們就須要一個分類表來存放商品的全部分類goots_category,可是這個外鍵是在商品goods表裏邊設置了一個category_id的外鍵,那爲何這樣呢!相對於商品分類,咱們的分類表是惟一的,咱們要是把商品表的id,存放在分類表裏邊,那麼分類表就很是大了,這樣不合理,對於查詢也會不友好的
3.商品的分類完了,在這個項目中咱們還有商品的屬性,但這個屬性是在分類下,而且還有一箇中間件表,來鏈接分類表跟商品屬性表
4.最後那就是咱們的商品庫存表了,在之前的操做裏邊咱們大多數都把庫存數據在商品表裏邊,可是根本就沒有考慮到屬性的存在,由於咱們的相對的屬性擁有的商品數量也不是同樣的,因此這個庫存表是是十分重要的。咱們能夠goods_sku這個表裏邊存放了, 咱們的商品id,副圖id,屬性id,屬性值id
那麼截止在這,咱們的數據庫就構思完了數據庫

數據庫遷移

商品表的遷移文件

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGoodsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('goods', function (Blueprint $table) {
            $table->increments('id')->comment('商品主鍵id');
            $table->integer('category_id')->comment('商品類別id');
            $table->string('image')->nullable()->comment('商品圖');
            $table->string('desc')->comment('商品描述');
            $table->bigInteger('state')->default(0)->comment('產品狀態 -1 已刪除  0 下架  1 上架');
            $table->dateTime('state_date')->nullable()->comment('商品上架時間');
            $table->integer('pv')->default(0)->comment('商品點擊量');
            $table->integer('sale')->default(0)->comment('銷售量');
            $table->integer('sort')->default(0)->comment('排序');
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('goods');
    }
}

商品分類表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGoodsCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('goods_categories', function (Blueprint $table) {
            $table->increments('id')->comment('商品類別主鍵id');
            $table->string('name')->comment('類別名稱');
            $table->integer('parent_id')->default(0)->comment('父級類別id');
            $table->string('image')->nullable()->comment('分類圖片');
            $table->integer('level')->default(0)->comment('分類等級');
            $table->integer('sort')->default(0)->comment('分類排序');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('goods_categories');
    }
}

副圖表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePrcturesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('prctures', function (Blueprint $table) {
            $table->increments('id')->comment('商品圖片主鍵id');
            $table->string('name')->comment('圖片名稱');
            $table->string('url')->comment('圖片地址');
            $table->integer('goods_id')->comment('商品id');
            $table->string('size')->comment('商品規格');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('prctures');
    }
}

商品屬性中間件表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGoodsAttributesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('goods_attributes', function (Blueprint $table) {
            $table->increments('id')->comment('商品屬性主鍵id');
            $table->string('name')->comment('商品屬性名');
            $table->integer('category_id')->comment('商品屬性id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('goods_attributes');
    }
}

屬性表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGoodsAttributeValuesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('goods_attribute_values', function (Blueprint $table) {
            $table->increments('id')->comment('商品屬性值:主鍵id');
            $table->string('name')->comment('商品屬性值:名稱');
            $table->integer('attribute_id')->comment('商品屬性值:名稱');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('goods_attribute_values');
    }
}

庫存表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGoodsSkusTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('goods_skus', function (Blueprint $table) {
            $table->increments('id')->comment('sku主鍵id');
            $table->integer('goods_id')->comment('商品id');
            $table->integer('attribute_id')->comment('屬性id');
            $table->integer('attribute_value_id')->comment('屬性值id');
            $table->integer('prcture_id')->comment('圖片id');
            $table->decimal('price', 8, 2)->default(0)->comment('價格');
            $table->integer('stock')->default(0)->comment('庫存');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('goods_skus');
    }
}

laravle的建立數據庫的代碼

命令 解釋
$table->bigIncrements(‘id’); 等同於自增 UNSIGNED BIGINT(主鍵)列
$table->bigInteger(‘votes’); 等同於 BIGINT 類型列
$table->binary(‘data’); 等同於 BLOB 類型列
$table->boolean(‘confirmed’); 等同於 BOOLEAN 類型列
$table->char(‘name’, 4); 等同於 CHAR 類型列
$table->date(‘created_at’); 等同於 DATE 類型列
$table->dateTime(‘created_at’); 等同於 DATETIME 類型列
$table->dateTimeTz(‘created_at’); 等同於 DATETIME 類型(帶時區)列
$table->decimal(‘amount’, 5, 2); 等同於 DECIMAL 類型列,帶精度和範圍
$table->double(‘column’, 15, 8); 等同於 DOUBLE 類型列,帶精度, 總共15位數字,小數點後8位
$table->enum(‘level’, [‘easy’, ‘hard’]); 等同於 ENUM 類型列
$table->float(‘amount’, 8, 2); 等同於 FLOAT 類型列,帶精度和總位數
$table->geometry(‘positions’); 等同於 GEOMETRY 類型列
$table->geometryCollection(‘positions’); 等同於 GEOMETRYCOLLECTION 類型列
$table->increments(‘id’); 等同於自增 UNSIGNED INTEGER (主鍵)類型列
$table->integer(‘votes’); 等同於 INTEGER 類型列
$table->ipAddress(‘visitor’); 等同於 IP 地址類型列
$table->json(‘options’); 等同於 JSON 類型列
$table->jsonb(‘options’); 等同於 JSONB 類型列
$table->lineString(‘positions’); 等同於 LINESTRING 類型列
$table->longText(‘description’); 等同於 LONGTEXT 類型列
$table->macAddress(‘device’); 等同於 MAC 地址類型列
$table->mediumIncrements(‘id’); 等同於自增 UNSIGNED MEDIUMINT 類型列(主鍵)
$table->mediumInteger(‘numbers’); 等同於 MEDIUMINT 類型列
$table->mediumText(‘description’); 等同於 MEDIUMTEXT 類型列
$table->morphs(‘taggable’); 添加一個 UNSIGNED INTEGER 類型的 taggable_id 列和一個 VARCHAR 類型的 taggable_type 列
$table->multiLineString(‘positions’); 等同於 MULTILINESTRING 類型列
$table->multiPoint(‘positions’); 等同於 MULTIPOINT 類型列
$table->multiPolygon(‘positions’); 等同於 MULTIPOLYGON 類型列
$table->nullableMorphs(‘taggable’); morphs() 列的 nullable 版本
$table->nullableTimestamps(); timestamps() 的別名
$table->point(‘position’); 等同於 POINT 類型列
$table->polygon(‘positions’); 等同於 POLYGON 類型列
$table->rememberToken(); 等同於添加一個容許爲空的 remember_token VARCHAR(100) 列
$table->smallIncrements(‘id’); 等同於自增 UNSIGNED SMALLINT (主鍵)類型列
$table->smallInteger(‘votes’); 等同於 SMALLINT 類型列
$table->softDeletes(); 新增一個容許爲空的 deleted_at TIMESTAMP 列用於軟刪除
$table->softDeletesTz(); 新增一個容許爲空的 deleted_at TIMESTAMP (帶時區)列用於軟刪除
$table->string(‘name’, 100); 等同於 VARCHAR 類型列,帶一個可選長度參數
$table->text(‘description’); 等同於 TEXT 類型列
$table->time(‘sunrise’); 等同於 TIME 類型列
$table->timeTz(‘sunrise’); 等同於 TIME 類型(帶時區)
$table->timestamp(‘added_on’); 等同於 TIMESTAMP 類型列
$table->timestampTz(‘added_on’); 等同於 TIMESTAMP 類型(帶時區)列
$table->timestamps(); 添加容許爲空的 created_at 和 updated_at TIMESTAMP 類型列
$table->timestampsTz(); 添加容許爲空的 created_at 和 updated_at TIMESTAMP 類型列(帶時區)
$table->tinyIncrements(‘numbers’); 等同於自增的 UNSIGNED TINYINT 類型列(主鍵)
$table->tinyInteger(‘numbers’); 等同於 TINYINT 類型列
$table->unsignedBigInteger(‘votes’); 等同於無符號的 BIGINT 類型列
$table->unsignedDecimal(‘amount’, 8, 2); 等同於 UNSIGNED DECIMAL 類型列,帶有總位數和精度
$table->unsignedInteger(‘votes’); 等同於無符號的 INTEGER 類型列
$table->unsignedMediumInteger(‘votes’); 等同於無符號的 MEDIUMINT 類型列
$table->unsignedSmallInteger(‘votes’); 等同於無符號的 SMALLINT 類型列
$table->unsignedTinyInteger(‘votes’); 等同於無符號的 TINYINT 類型列
$table->uuid(‘id’); 等同於 UUID 類型列
$table->year(‘birth_year’); 等同於 YEAR 類型列
相關文章
相關標籤/搜索