php - Laravel 关系有很多
问题描述
我对 Laravel 的关系有一些问题。我有 3 个需要关联的表,问题是在某些产品上它可以工作,而在另一个产品上却不行,我不知道为什么。所以我有3个表:产品,包(有关系表),packages_info Products hasMany packages和packages hasMany packages_info,真正的问题是packages_info,在一个产品中提供信息,但在另一个产品中,它没有:/所以在这里是我的模型:
产品型号:
受保护的 $table = '产品';
public function packages() { return $this->hasMany(ProductPackages::class, 'product_id', 'id'); }
移民:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('sku');
$table->string('slug');
$table->set('type', ['product', 'accessory']);
$table->timestamps();
});
- 产品包是产品和包之间的关系
模型:
protected $table = 'products_packages';
public function info()
{
return $this->hasMany('App\Models\Product\PackageInfo');
}
移民:
Schema::create('products_packages', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_id');
$table->unsignedBigInteger('package_id');
$table->integer('qty')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('package_id')->references('id')->on('packages');
});
和包裹信息:
模型:
protected $table = 'package_info';
public function package() // no use right now
{
return $this->belongsTo(Packages::class);
}
迁移:
Schema::create('package_info', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_packages_id');
$table->unsignedBigInteger('country_id');
$table->string('name');
$table->string('slug');
$table->integer('qty')->nullable();
$table->float('real_price', 8, 2);
$table->float('sale_price', 8, 2)->nullable();
$table->float('delivery_price', 8, 2);
$table->text('image');
$table->boolean('preselected')->default(false);
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('product_packages_id')->references('id')->on('packages');
});
所以这里的想法是获取带有 id 的产品,显示关系表,并从该关系中获取所有 package_info 并列出它们(如您所见,关系是 hasMany)。所以这是我在控制器中的操作方式:
Products::where('id', $id)->with('packages.info')->firstOrFail();
当我尝试获得其中一种添加的产品时,我需要的输出是这样的:
"packages": [
{
"id": 1,
"product_id": 1,
"package_id": 1,
"qty": null,
"created_at": "2021-03-24T15:53:44.000000Z",
"updated_at": "2021-03-24T15:53:44.000000Z",
"info": [
{
"id": 1,
"product_packages_id": 1,
"country_id": 68,
"name": "First Product",
"slug": "es_1xproduct",
"qty": 1,
"real_price": 67,
"sale_price": 43.55,
"delivery_price": 4.5,
"image": "https://domainas.com/web/images/order-img1.jpg",
"preselected": 0,
"created_at": "2021-03-24T15:53:44.000000Z",
"updated_at": "2021-03-24T15:53:44.000000Z"
},
{
"id": 6,
"product_packages_id": 1,
"country_id": 179,
"name": "1x Product",
"slug": "pl_1xproduct",
"qty": 1,
"real_price": 225,
"sale_price": 146.25,
"delivery_price": 10,
"image": "https://domainas.com/web/images/order-img1.jpg",
"preselected": 0,
"created_at": "2021-03-24T15:53:44.000000Z",
"updated_at": "2021-03-24T15:53:44.000000Z"
}
]
}
]
所以这是我拥有和需要的输出,但例如,在另一个产品中,它是不正确的。它会给我产品和包之间的正确关系,但它不会给我包和 package_info 之间的正确关系。任何想法可能是什么问题?
解决方案
推荐阅读
- java - Is it safe to give a temporary address ( assigned in a While block ) to a global ListNode?
- petrel - 在 PETREL 中计算给定井附近区域的地震属性
- javascript - Javascript - get specified bit value as int without using Number(n).toString(2)
- python - 如何在数学中使用base2
- c# - 为 .Net Core 选择正确版本的 GCP 数据存储库
- html - 如何从具有多种形式的角度应用程序的 html 页面中删除该行
- java - Kafka-Connect 将 SQL JAR 文件添加到类路径
- android - 无法在 ImageView 中显示图像
- javascript - html 像布局引擎一样导入
- javascript - 输入值不接受vue js上的对象