Laravel 5 product order relation syntax: Not unique table/alias
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
add a comment |
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
I think you got confused on the relationships. your haveProduct->belongsTo
when Product should be your beacon class, meaning it should owncategory
andnot 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 aProduct
can ownone or more
Categories` which will present you with a one to many relationships that in this case will result in 3 tables just forProduct
andCategories
relationship;
– Diogo Santo
Nov 22 '18 at 13:31
On the other hand, could you elaborate why the need forPriceOrderProduct
andPriceOrder
? 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
add a comment |
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
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
php laravel-5 eloquent syntax-error relationship
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 haveProduct->belongsTo
when Product should be your beacon class, meaning it should owncategory
andnot 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 aProduct
can ownone or more
Categories` which will present you with a one to many relationships that in this case will result in 3 tables just forProduct
andCategories
relationship;
– Diogo Santo
Nov 22 '18 at 13:31
On the other hand, could you elaborate why the need forPriceOrderProduct
andPriceOrder
? 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
add a comment |
I think you got confused on the relationships. your haveProduct->belongsTo
when Product should be your beacon class, meaning it should owncategory
andnot 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 aProduct
can ownone or more
Categories` which will present you with a one to many relationships that in this case will result in 3 tables just forProduct
andCategories
relationship;
– Diogo Santo
Nov 22 '18 at 13:31
On the other hand, could you elaborate why the need forPriceOrderProduct
andPriceOrder
? 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
add a comment |
1 Answer
1
active
oldest
votes
<?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.
the hasMany did the job! Thanks!
– Dennis
Nov 22 '18 at 16:11
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
<?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.
the hasMany did the job! Thanks!
– Dennis
Nov 22 '18 at 16:11
add a comment |
<?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.
the hasMany did the job! Thanks!
– Dennis
Nov 22 '18 at 16:11
add a comment |
<?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.
<?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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I think you got confused on the relationships. your have
Product->belongsTo
when Product should be your beacon class, meaning it should owncategory
andnot 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 ownone or more
Categories` which will present you with a one to many relationships that in this case will result in 3 tables just forProduct
andCategories
relationship;– Diogo Santo
Nov 22 '18 at 13:31
On the other hand, could you elaborate why the need for
PriceOrderProduct
andPriceOrder
? 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