6
0
Fork 0

添加商品分区模型

release
李静 2021-11-29 11:45:18 +08:00
parent feb96bab97
commit 5126e613aa
7 changed files with 222 additions and 1 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductPart extends Model
{
use Concerns\HasShowable;
use HasFactory;
/**
* @var array
*/
protected $attributes = [
'is_show' => false,
];
/**
* @var array
*/
protected $fillable = [
'key',
'name',
'is_show',
];
/**
* @var array
*/
protected $casts = [
'is_show' => 'bool',
];
/**
* 属于此分区的商品
*/
public function skus()
{
return $this->belongsToMany(ProductSku::class, ProductPartSku::class, 'part_id', 'sku_id');
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Models;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductPartSku extends Model
{
use Filterable;
use HasFactory;
/**
* @var string
*/
protected $table = 'product_part_sku';
/**
* @var array
*/
protected $attributes = [
'sort' => 0,
];
/**
* @var array
*/
protected $fillable = [
'part_id',
'sku_id',
'sort',
];
/**
* 此分区商品所属的SKU
*/
public function sku()
{
return $this->belongsTo(ProductSku::class, 'sku_id');
}
/**
* 此分区商品所属的分区
*/
public function part()
{
return $this->belongsTo(ProductPart::class, 'part_id');
}
}

View File

@ -13,6 +13,9 @@ class ProductSku extends Model
use Filterable;
use HasDateTimeFormatter;
/**
* @var array
*/
protected $casts = [
'images' => JsonArray::class,
'sell_price' => Price::class,
@ -46,4 +49,31 @@ class ProductSku extends Model
'sales',
'release_at',
];
/**
* 仅查询已上架的商品
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsRelease($query)
{
return $query->whereNotNull('release_at');
}
/**
* 此商品所属的分类
*/
public function category()
{
return $this->belongsTo(ProductCategory::class, 'category_id');
}
/**
* 此商品所属的分区
*/
public function parts()
{
return $this->belongsToMany(ProductPart::class, ProductPartSku::class, 'sku_id', 'part_id');
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductPartsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_parts', function (Blueprint $table) {
$table->id();
$table->string('key')->unique()->comment('分区KEY');
$table->string('name')->comment('名称');
$table->boolean('is_show')->default(false)->comment('是否显示0 不显示1 显示');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_parts');
}
}

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductPartSkuTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_part_sku', function (Blueprint $table) {
$table->id();
$table->foreignId('part_id')->constrained('product_parts')->onDelete('cascade')->comment('分区 ID');
$table->foreignId('sku_id')->constrained('product_skus')->onDelete('cascade')->comment('商品 ID');
$table->integer('sort')->default(0)->comment('排序');
$table->timestamps();
$table->unique(['part_id', 'sku_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_part_sku');
}
}

View File

@ -13,6 +13,8 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// \App\Models\User::factory(10)->create();
$this->call([
ProductPartSeeder::class,
]);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Database\Seeders;
use App\Models\ProductPart;
use Illuminate\Database\Seeder;
class ProductPartSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
foreach ([
'recommend' => [
'name' => '精品推荐',
'is_show' => true,
],
] as $key => $values) {
ProductPart::firstOrCreate(['key' => $key], $values);
}
}
}