代码之家  ›  专栏  ›  技术社区  ›  Dennis

Laravel 5产品订单关系语法:非唯一表/别名

  •  0
  • Dennis  · 技术社区  · 7 年前


    简言之 :餐厅有一个价目表,他们可以通过在其中插入新价格的流程来更新该价目表。

    因此,将生成价格订单。

    检索产品的所有类别 (没问题)然后我想做一个 从产品到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_ “我的表”的前缀)

    表:价格\产品\类别
    -身份证
    类别信息等。

    型号:ProductCategory

    public function products()
    {
        return $this->hasMany(Product::class, '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');
    }
    

    ==========================
    表:订单价格
    -身份证
    -餐厅id

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


    表价格\订单\产品
    -订单号
    -产品标识
    -价格
    -产品信息

    型号:PriceOrderProduct

    public function orders()
    {
        return $this->belongsTo(PriceOrder::class);
    }
    
    public function products()
    {
        return $this->hasMany(Product::class, 'id', 'product_id');
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Peter Steenbergen    7 年前
    <?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');
        }
    }
    

    这将用于查询所需的输出。通过得到一个有很多关系而不是一个归属感的人。

    推荐文章