这一篇主要写商品数据库的设计,以及数据库迁移文件的创建
在之前的文章中,我们把前台的登录已经做完了,那么作为一个商城项目最重要的就是商品了,下来咱们就对商品进行创建数据库,以及数据迁移文件的使用
商品数据库的设计
思路:
我们在创建商品表的时候需要先了解商品都有什么属性,这里我直接就把已经设计好的模型放在这里了,然后咱们进行分析
1.我们都知道我们的商品是分主图跟副图之分的,我们的主图是现实在首页或者分类页面的数据,副图是存在于商品详情页,所以我们把副图的数据新建一个表存放,然后在添加一个商品id的外键用来关联商品,这就是我们picture表存在的意义
2.我们的商品除了在首页显示的,其余的应该都是在分类下,那么我们就需要一个分类表来存放商品的所有分类goots_category,但是这个外键是在商品goods表里边设置了一个category_id的外键,那为什么这样呢!相对于商品分类,我们的分类表是唯一的,我们要是把商品表的id,存放在分类表里边,那么分类表就非常大了,这样不合理,对于查询也会不友好的
3.商品的分类完了,在这个项目中我们还有商品的属性,但这个属性是在分类下,并且还有一个中间件表,来连接分类表跟商品属性表
4.最后那就是我们的商品库存表了,在以前的操作里边我们大多数都把库存数据在商品表里边,但是根本就没有考虑到属性的存在,因为我们的相对的属性拥有的商品数量也不是一样的,所以这个库存表是是十分重要的。我们可以goods_sku这个表里边存放了, 我们的商品id,副图id,属性id,属性值id
那么截止在这,我们的数据库就构思完了
数据库迁移
商品表的迁移文件
<?phpuse 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');}}
商品分类表
<?phpuse 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');}}
副图表
<?phpuse 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');}}
商品属性中间件表
<?phpuse 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');}}
属性表
<?phpuse 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');}}
库存表
<?phpuse 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 类型列 | 
