【发布时间】:2021-05-31 21:06:24
【问题描述】:
我正在努力构建一个 Laravel API,但我在模型关系方面遇到了问题,并且不确定我是否做得很好。
我的项目中有以下模型:
- 品牌
- 类别
- 亲切
- 产品
- 尺寸
- 产品图片
这些模型代表电子商务上可用的产品,我试图通过以下方式将其关联起来:
1 个产品有 1 个品牌 + 1 个类别 + 1 个种类 + n 个 ProductImages(每个图像的一个产品 n 个图像只属于一个产品)+ n 个尺寸(我想为多个寄存器使用相同尺寸的表格行)。
我希望这与数据库相关,因此我可以从 1 个产品中获取所有相关信息,例如品牌或类别,但我还需要从一个品牌反向获取所有产品。
所以我开始做以下模型功能:
品牌:
public function product(){
return $this->hasMany(Product::class);
}
类别:
public function product(){
return $this->hasMany(Product::class);
}
种类:
public function product(){
return $this->hasMany(Product::class);
}
产品:
public function category(){
return $this->belongsTo(Category::class);
}
public function brand(){
return $this->belongsTo(Brand::class);
}
public function size(){
$this->belongsTo(Size::class);
}
public function kind(){
return $this->belongsTo(Kind::class);
}
public function productImages(){
return $this->hasMany(ProductImages::class);
}
尺寸:
public function product(){
$this->belongsTo(Product::class);
}
产品图片:
public function product(){
$this->belongsTo(Product::class);
}
如果我在 Tinker 上查询类别,我会得到类别列表,但如果我尝试获取属于同一类别的所有产品,我什么也得不到,但不确定是否像我正在做的那样关联所有模型.
当我在 tinker 上查看产品品牌时,我得到的是:
>>> $product = App\Models\Product::First();
=> App\Models\Product {#4307
id: "1",
descrption: "Voluptatibus et eius enim aut ipsa earum quis veritatis.",
productImages_id: null,
size_id: null,
brand_id: "1",
category_id: null,
kind_id: "1",
created_at: "2021-03-01 11:19:18",
updated_at: "2021-03-02 10:26:14",
}
>>> $product->brand();
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#4302}
迁移:
产品表:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('descrption');
$table->integer('productImages_id')->nullable();
$table->integer('size_id')->nullable();
$table->integer('brand_id')->nullable();
$table->integer('category_id')->nullable();
$table->integer('kind_id')->nullable();
$table->timestamps();
});
}
品牌表:
public function up()
{
Schema::create('brands', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
类别表:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category');
$table->timestamps();
});
}
尺码表:
public function up()
{
Schema::create('sizes', function (Blueprint $table) {
$table->id();
$table->string('size');
$table->integer('product_id');
$table->timestamps();
});
}
种类表:
public function up()
{
Schema::create('kinds', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('kind');
$table->timestamps();
});
}
ProductImages 表:
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('url');
$table->timestamps();
});
}
【问题讨论】:
-
所有键名是否都符合 laravel 标准?
-
是的。如果也分享迁移会有用吗?
-
@TarangP 我添加了迁移文件
-
不需要发布这么多代码。请尽量用尽可能少的文字来减少您的问题。它将帮助您更好地思考问题并帮助其他人审查问题
标签: php laravel eloquent model tinker