【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 类型列
相关文章
相关标签/搜索