php - Laravel 的新手使用关系模型
问题描述
我正在努力建立一个 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();
});
}
产品图片表:
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('url');
$table->timestamps();
});
}
解决方案
经过几次修改后,问题似乎来自迁移文件,因为引用外键的列被设置为整数以存储 id 并将其设置为 unsignedInteger 并重新运行迁移解决了该问题。这是 Products 表迁移文件:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('descrption');
$table->unsignedInteger('size_id')->nullable();
$table->unsignedInteger('brand_id')->nullable();
$table->unsignedInteger('category_id')->nullable();
$table->unsignedInteger('kind_id')->nullable();
$table->timestamps();
});
}
推荐阅读
- python - 我无法播放 .wav 文件并播放 Windows 警报声
- java - 匹配的盖子和罐子实施
- javascript - 如果 asp.net 按钮已启用 =“false”,则在执行代码隐藏之前它不会调用 javascript 函数
- android - 如何正确地将数据从 recyclerView 获取到设置了 RV 的 Activity?
- google-maps - Flutter for Web 中的 WebView 未加载 Google 地图或其他
- c++ - 如何解决错误(项目名称已触发断点)?
- java - Gradle 构建错误:提供的字符串模块表示法
- python - 网络抓取:获取图表背后的数据
- php - 使用 preg_replace 在单词前后提取部分字符串
- c# - 在 mac 上安装单声道/提琴手