我尝试运行$customer->competitorProduct()->where("product_id", 2)->get()
时出错
我在表中发现错误,字段“”中不存在,同时检查Laravel中的查询类似于以下内容:
SELECT `competitor_product`.*,
`competitor_product_customer`.`id` AS `pivot_id`,
`competitor_product_customer`.`customer_id` AS `pivot_customer_id`,
`competitor_product_customer`.`competitor_product_id` AS `pivot_competitor_product_id`,
`competitor_product_customer`.`` AS `pivot_`,
`competitor_product_customer`.`status` AS `pivot_status`,
`competitor_product_customer`.`created_at` AS `pivot_created_at`,
`competitor_product_customer`.`updated_at` AS `pivot_updated_at`
FROM `competitor_product`
INNER JOIN `competitor_product_customer`
ON `competitor_product`.`id` = `competitor_product_customer`.``
WHERE `competitor_product_customer`.`customer_id` = 1
AND `product_id` = 2
我与某些表有N.N关系:
DER Image
https://i.stack.imgur.com/Zh4pV.png
<?php
...
class Product extends Model
{
public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class)
->using(CustomerProduct::class)
->as("customerProduct")
->withTimestamps()
->withPivot([
"id",
"customer_id",
"product_id",
...
]);
}
public function competitors(): BelongsToMany
{
return $this->belongsToMany(Competitor::class)
->using(CompetitorProduct::class)
->as("competitorProduct")
->withTimestamps()
->withPivot([
"id",
"product_id",
"competitor_id",
...
...
]);
}
}
<?php
...
class Customer extends Model
{
public function competitorProduct()
{
return $this->belongsToMany(CompetitorProduct::class)
->using(CompetitorProductCustomer::class)
->as('competitorProductCustomer')
->withTimestamps();
->withPivot([
'id',
'customer_id',
'competitor_product_id',
'status'
]);
}
public function products()
{
return $this->belongsToMany(Product::class)
->using(CustomerProduct::class)
->as('customerProduct')
->withPivot([
'id',
'customer_id',
'product_id',
...
]);
}
}
竞争对手:complete code
<?php
...
class Competitor extends Model
{
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class)
->using(CompetitorProduct::class)
->as("competitorProduct")
->withTimestamps()
->withPivot([
"id",
"product_id",
"competitor_id",
...
]);
}
}
客户产品数据中心:complete code
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Carbon;
class CustomerProduct extends Pivot
{
}
竞争对手产品枢纽:complete code
<?php
...
class CompetitorProduct extends Pivot
{
public function customers()
{
return $this->belongsToMany(Customer::class)
->using(CompetitorProductCustomer::class)
->as('competitorProductCustomer')
->withTimestamps();
->withPivot([
'id',
'customer_id',
'competitor_product_id',
'status'
]);
}
}
竞争对手产品x客户产品枢轴:complete code
<?php
...
class CompetitorProductCustomer extends Pivot
{
}
我在犯一些错误, 或银行关系或有雄辩的关系
有解决方案吗?