【问题标题】:Laravel 5 product order relation syntax: Not unique table/aliasLaravel 5 产品订单关系语法:不是唯一的表/别名
【发布时间】:2019-04-25 01:43:42
【问题描述】:

我正在开发一个可用于为产品订购价目表的应用程序。
简而言之:一家餐厅有一个价目表,他们可以按照他们所在的流程进行更新可以插入新价格。

结果将生成价格订单。

现在我想要的是使用产品检索所有类别(没问题),然后我想建立一个产品与 PriceOrderProduct 的关系,这样我就知道什么时候产品在订单中使用。

我现在有这个:

ProductCategory::with('products.orderProduct')->orderBy('order')->get() 

这给了我这个错误:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 

完全错误:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'price_order_products' (SQL: select `price_order_products`.*, `price_order_products`.`product_id` as `pivot_product_id`, `price_order_products`.`id` as `pivot_id` from `price_order_products` inner join `price_order_products` on `price_order_products`.`id` = `price_order_products`.`id` where `price_order_products`.`product_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11))

我已经搜索过这个错误,但不知道如何解决,有人可以帮我解决这个问题吗?

这些是我的表格和关系:

(我为我的表格使用了price_ 前缀)

表格:price_product_categories
- 身份证
类别信息等。

型号:产品类别

public function products()
{
    return $this->hasMany(Product::class, 'product_category_id');
}

==========================
price_products
- 身份证
- product_category_id
产品信息等

型号:产品

public function categories()
{
    return $this->belongsTo(ProductCategory::class, 'product_category_id');
}


public function orderProduct()
{
    return $this->belongsToMany(PriceOrderProduct::class, 'price_order_products', 'product_id', 'id');
}

===========================
表格:price_orders
- 身份证
- restaurant_id
等等。

型号:PriceOrder

public function products()
{
    return $this->hasMany(PriceOrderProduct::class, 'order_id');
}

===========================
表 price_order_products
- order_id
- product_id
- 价格
- 产品信息

型号:PriceOrderProduct

public function orders()
{
    return $this->belongsTo(PriceOrder::class);
}

public function products()
{
    return $this->hasMany(Product::class, 'id', 'product_id');
}

【问题讨论】:

  • 我认为您对这些关系感到困惑。当 Product 应该是你的信标类时,你有 Product->belongsTo,这意味着它应该拥有 categorynot be owned by a category
  • 您的数据库结构也可以不同的方式构建并适应未来的变化,但这取决于您的产品在哪里增长。例如,你会发现你的产品只拥有一种类别吗?还是他们可能拥有一个或多个类别?如果最新消息敲响了警钟和未来的可能性,那么我强烈建议重组您的模型和数据库关系,其中 Product 可以拥有 one or more 类别`,这将为您提供一对多的关系,在这种情况下将导致在 3 个表中仅用于 ProductCategories 关系;
  • 另一方面,您能否详细说明为什么需要PriceOrderProductPriceOrder?他们也可以更好地命名为恕我直言,以快速解决表格的目标及其关系:) 在我将其设置为石头后,我将能够提供更多帮助
  • @DiogoSanto 我重构了一些雄辩的模型和数据库表名。现在最好阅读关系。彼得也为我提供了解决方案。感谢您的回复。

标签: php laravel-5 eloquent syntax-error relationship


【解决方案1】:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'price_products';

    public function categories()
    {
        return $this->belongsTo(ProductCategory::class, 'product_category_id');
    }

    public function orderProducts()
    {
        return $this->hasMany(PriceOrderProduct::class, 'product_id');
    }
}

这将适用于您对所需输出的查询。通过获取 hasMany 关系而不是 belongsToMany。

【讨论】:

  • hasMany 完成了这项工作!谢谢!
猜你喜欢
  • 2018-09-28
  • 1970-01-01
  • 2017-09-17
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
  • 2019-03-15
  • 1970-01-01
相关资源
最近更新 更多