Laravel 5 product order relation syntax: Not unique table/alias












0















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









share|improve this question

























  • I think you got confused on the relationships. your have Product->belongsTo when Product should be your beacon class, meaning it should own category and not be owned by a category

    – Diogo Santo
    Nov 22 '18 at 11:50













  • Also your DB structure can be built differently and accomodate future changes, but it depends on where your products are growing. For instance, would you recon your product only owns 1 type of category? Or is it probable they can own one or more categories? If latest rings a bell and a future possibility, then I would highly recommend to restructure your models and database relationships where a Product can own one or more Categories` which will present you with a one to many relationships that in this case will result in 3 tables just for Product and Categories relationship;

    – Diogo Santo
    Nov 22 '18 at 13:31











  • On the other hand, could you elaborate why the need for PriceOrderProduct and PriceOrder? Also they could be better named imho, to quickly settled the objective of the tables and their relationships :) I will be able to help out more after I set this into stone

    – Diogo Santo
    Nov 22 '18 at 13:32











  • @DiogoSanto I refactored some of my eloquent models and database tablenames. Now it is better to read the relations. Also Peter had the solution for me. Thanks for your reply.

    – Dennis
    Nov 22 '18 at 16:15
















0















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









share|improve this question

























  • I think you got confused on the relationships. your have Product->belongsTo when Product should be your beacon class, meaning it should own category and not be owned by a category

    – Diogo Santo
    Nov 22 '18 at 11:50













  • Also your DB structure can be built differently and accomodate future changes, but it depends on where your products are growing. For instance, would you recon your product only owns 1 type of category? Or is it probable they can own one or more categories? If latest rings a bell and a future possibility, then I would highly recommend to restructure your models and database relationships where a Product can own one or more Categories` which will present you with a one to many relationships that in this case will result in 3 tables just for Product and Categories relationship;

    – Diogo Santo
    Nov 22 '18 at 13:31











  • On the other hand, could you elaborate why the need for PriceOrderProduct and PriceOrder? Also they could be better named imho, to quickly settled the objective of the tables and their relationships :) I will be able to help out more after I set this into stone

    – Diogo Santo
    Nov 22 '18 at 13:32











  • @DiogoSanto I refactored some of my eloquent models and database tablenames. Now it is better to read the relations. Also Peter had the solution for me. Thanks for your reply.

    – Dennis
    Nov 22 '18 at 16:15














0












0








0


0






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









share|improve this question
















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






php laravel-5 eloquent syntax-error relationship






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:29







Dennis

















asked Nov 22 '18 at 11:21









DennisDennis

15012




15012













  • I think you got confused on the relationships. your have Product->belongsTo when Product should be your beacon class, meaning it should own category and not be owned by a category

    – Diogo Santo
    Nov 22 '18 at 11:50













  • Also your DB structure can be built differently and accomodate future changes, but it depends on where your products are growing. For instance, would you recon your product only owns 1 type of category? Or is it probable they can own one or more categories? If latest rings a bell and a future possibility, then I would highly recommend to restructure your models and database relationships where a Product can own one or more Categories` which will present you with a one to many relationships that in this case will result in 3 tables just for Product and Categories relationship;

    – Diogo Santo
    Nov 22 '18 at 13:31











  • On the other hand, could you elaborate why the need for PriceOrderProduct and PriceOrder? Also they could be better named imho, to quickly settled the objective of the tables and their relationships :) I will be able to help out more after I set this into stone

    – Diogo Santo
    Nov 22 '18 at 13:32











  • @DiogoSanto I refactored some of my eloquent models and database tablenames. Now it is better to read the relations. Also Peter had the solution for me. Thanks for your reply.

    – Dennis
    Nov 22 '18 at 16:15



















  • I think you got confused on the relationships. your have Product->belongsTo when Product should be your beacon class, meaning it should own category and not be owned by a category

    – Diogo Santo
    Nov 22 '18 at 11:50













  • Also your DB structure can be built differently and accomodate future changes, but it depends on where your products are growing. For instance, would you recon your product only owns 1 type of category? Or is it probable they can own one or more categories? If latest rings a bell and a future possibility, then I would highly recommend to restructure your models and database relationships where a Product can own one or more Categories` which will present you with a one to many relationships that in this case will result in 3 tables just for Product and Categories relationship;

    – Diogo Santo
    Nov 22 '18 at 13:31











  • On the other hand, could you elaborate why the need for PriceOrderProduct and PriceOrder? Also they could be better named imho, to quickly settled the objective of the tables and their relationships :) I will be able to help out more after I set this into stone

    – Diogo Santo
    Nov 22 '18 at 13:32











  • @DiogoSanto I refactored some of my eloquent models and database tablenames. Now it is better to read the relations. Also Peter had the solution for me. Thanks for your reply.

    – Dennis
    Nov 22 '18 at 16:15

















I think you got confused on the relationships. your have Product->belongsTo when Product should be your beacon class, meaning it should own category and not be owned by a category

– Diogo Santo
Nov 22 '18 at 11:50







I think you got confused on the relationships. your have Product->belongsTo when Product should be your beacon class, meaning it should own category and not be owned by a category

– Diogo Santo
Nov 22 '18 at 11:50















Also your DB structure can be built differently and accomodate future changes, but it depends on where your products are growing. For instance, would you recon your product only owns 1 type of category? Or is it probable they can own one or more categories? If latest rings a bell and a future possibility, then I would highly recommend to restructure your models and database relationships where a Product can own one or more Categories` which will present you with a one to many relationships that in this case will result in 3 tables just for Product and Categories relationship;

– Diogo Santo
Nov 22 '18 at 13:31





Also your DB structure can be built differently and accomodate future changes, but it depends on where your products are growing. For instance, would you recon your product only owns 1 type of category? Or is it probable they can own one or more categories? If latest rings a bell and a future possibility, then I would highly recommend to restructure your models and database relationships where a Product can own one or more Categories` which will present you with a one to many relationships that in this case will result in 3 tables just for Product and Categories relationship;

– Diogo Santo
Nov 22 '18 at 13:31













On the other hand, could you elaborate why the need for PriceOrderProduct and PriceOrder? Also they could be better named imho, to quickly settled the objective of the tables and their relationships :) I will be able to help out more after I set this into stone

– Diogo Santo
Nov 22 '18 at 13:32





On the other hand, could you elaborate why the need for PriceOrderProduct and PriceOrder? Also they could be better named imho, to quickly settled the objective of the tables and their relationships :) I will be able to help out more after I set this into stone

– Diogo Santo
Nov 22 '18 at 13:32













@DiogoSanto I refactored some of my eloquent models and database tablenames. Now it is better to read the relations. Also Peter had the solution for me. Thanks for your reply.

– Dennis
Nov 22 '18 at 16:15





@DiogoSanto I refactored some of my eloquent models and database tablenames. Now it is better to read the relations. Also Peter had the solution for me. Thanks for your reply.

– Dennis
Nov 22 '18 at 16:15












1 Answer
1






active

oldest

votes


















1














<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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


This will work for your query for the desired output. By getting a hasMany relation and not a belongsToMany.






share|improve this answer
























  • the hasMany did the job! Thanks!

    – Dennis
    Nov 22 '18 at 16:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429846%2flaravel-5-product-order-relation-syntax-not-unique-table-alias%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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


This will work for your query for the desired output. By getting a hasMany relation and not a belongsToMany.






share|improve this answer
























  • the hasMany did the job! Thanks!

    – Dennis
    Nov 22 '18 at 16:11
















1














<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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


This will work for your query for the desired output. By getting a hasMany relation and not a belongsToMany.






share|improve this answer
























  • the hasMany did the job! Thanks!

    – Dennis
    Nov 22 '18 at 16:11














1












1








1







<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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


This will work for your query for the desired output. By getting a hasMany relation and not a belongsToMany.






share|improve this answer













<?php

namespace App;

use IlluminateDatabaseEloquentModel;

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


This will work for your query for the desired output. By getting a hasMany relation and not a belongsToMany.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 13:48









Peter SteenbergenPeter Steenbergen

1157




1157













  • the hasMany did the job! Thanks!

    – Dennis
    Nov 22 '18 at 16:11



















  • the hasMany did the job! Thanks!

    – Dennis
    Nov 22 '18 at 16:11

















the hasMany did the job! Thanks!

– Dennis
Nov 22 '18 at 16:11





the hasMany did the job! Thanks!

– Dennis
Nov 22 '18 at 16:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429846%2flaravel-5-product-order-relation-syntax-not-unique-table-alias%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga