jeudi 22 novembre 2018

Laravel 5 product order relation

I'm working on an applications which can be used to make pricelist orders for products.
In short: a restaurant has a pricelist, which they can update by following a flow where they can insert new prices.

As result a price order will be generated.

Now what I want is that I retrieve all categories with products (no problem) and then I want to make a relation from product to the PriceOrderProduct so I know when a product is used in an order.

I have this now:

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

which gaves me this error:

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

complete error:

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))

I have searched for this error, but don't know how to fix, can someone help me out with this issue?

These are my tables and relations:

(I make use of a price_ prefix for my tables)

Table: price_product_categories
- id
category info etc.

Model: ProductCategory

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

==========================
price_products
- id
- product_category_id
product info etc.

Model: Product

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');
}

==========================
Table: price_orders
- id
- restaurant_id
etc.

Model: PriceOrder

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

==========================
Table price_order_products
- order_id
- product_id
- price
- product_info

Model: PriceOrderProduct

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

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire