Laravel Left join multiple tables and return the original table value
I'm having issues with joining three tables and getting the original table value back.
I have a parent table which is a store that needs to find the brand_id for the products within the store. I can't access the brand_id directly so I need to join, I have to join on to the stores product list then join that onto the product table which holds the brand_id.
$this_return = Store::with('address','setting')
->leftjoin('StoreProducts', function ($join){
$join->on('StoreProducts.store_id', '=', 'stores.id');
})->leftjoin('products', function ($join) {
$join->on('products.id','=','StoreProducts.product_id');
})
->where('products.brand_id', '=', $brandID)
->isActive()
->get();
This is returning a product value, But I wanted it to return all the stores the original table back if the products.brand_id was equal to the $brandID then return the current store and repeat for all stores.
Hope that makes sense
Any help would be great.
laravel eloquent
add a comment |
I'm having issues with joining three tables and getting the original table value back.
I have a parent table which is a store that needs to find the brand_id for the products within the store. I can't access the brand_id directly so I need to join, I have to join on to the stores product list then join that onto the product table which holds the brand_id.
$this_return = Store::with('address','setting')
->leftjoin('StoreProducts', function ($join){
$join->on('StoreProducts.store_id', '=', 'stores.id');
})->leftjoin('products', function ($join) {
$join->on('products.id','=','StoreProducts.product_id');
})
->where('products.brand_id', '=', $brandID)
->isActive()
->get();
This is returning a product value, But I wanted it to return all the stores the original table back if the products.brand_id was equal to the $brandID then return the current store and repeat for all stores.
Hope that makes sense
Any help would be great.
laravel eloquent
Do you want the stores which contains a brand?
– Taha Paksu
Nov 26 '18 at 13:48
I want to filter out the stores with a specific brand, but to find out if the store has the specific brand of product I have to go through two tables and match ids. The store table does not contain a brand_id it has a store_id, the StoreProducts has a store_id and product_id and the product table has a id and brand_id.
– Orbitalcoder
Nov 26 '18 at 14:00
add a comment |
I'm having issues with joining three tables and getting the original table value back.
I have a parent table which is a store that needs to find the brand_id for the products within the store. I can't access the brand_id directly so I need to join, I have to join on to the stores product list then join that onto the product table which holds the brand_id.
$this_return = Store::with('address','setting')
->leftjoin('StoreProducts', function ($join){
$join->on('StoreProducts.store_id', '=', 'stores.id');
})->leftjoin('products', function ($join) {
$join->on('products.id','=','StoreProducts.product_id');
})
->where('products.brand_id', '=', $brandID)
->isActive()
->get();
This is returning a product value, But I wanted it to return all the stores the original table back if the products.brand_id was equal to the $brandID then return the current store and repeat for all stores.
Hope that makes sense
Any help would be great.
laravel eloquent
I'm having issues with joining three tables and getting the original table value back.
I have a parent table which is a store that needs to find the brand_id for the products within the store. I can't access the brand_id directly so I need to join, I have to join on to the stores product list then join that onto the product table which holds the brand_id.
$this_return = Store::with('address','setting')
->leftjoin('StoreProducts', function ($join){
$join->on('StoreProducts.store_id', '=', 'stores.id');
})->leftjoin('products', function ($join) {
$join->on('products.id','=','StoreProducts.product_id');
})
->where('products.brand_id', '=', $brandID)
->isActive()
->get();
This is returning a product value, But I wanted it to return all the stores the original table back if the products.brand_id was equal to the $brandID then return the current store and repeat for all stores.
Hope that makes sense
Any help would be great.
laravel eloquent
laravel eloquent
edited Nov 26 '18 at 16:46
Priyanka khullar
543212
543212
asked Nov 26 '18 at 13:28
OrbitalcoderOrbitalcoder
32
32
Do you want the stores which contains a brand?
– Taha Paksu
Nov 26 '18 at 13:48
I want to filter out the stores with a specific brand, but to find out if the store has the specific brand of product I have to go through two tables and match ids. The store table does not contain a brand_id it has a store_id, the StoreProducts has a store_id and product_id and the product table has a id and brand_id.
– Orbitalcoder
Nov 26 '18 at 14:00
add a comment |
Do you want the stores which contains a brand?
– Taha Paksu
Nov 26 '18 at 13:48
I want to filter out the stores with a specific brand, but to find out if the store has the specific brand of product I have to go through two tables and match ids. The store table does not contain a brand_id it has a store_id, the StoreProducts has a store_id and product_id and the product table has a id and brand_id.
– Orbitalcoder
Nov 26 '18 at 14:00
Do you want the stores which contains a brand?
– Taha Paksu
Nov 26 '18 at 13:48
Do you want the stores which contains a brand?
– Taha Paksu
Nov 26 '18 at 13:48
I want to filter out the stores with a specific brand, but to find out if the store has the specific brand of product I have to go through two tables and match ids. The store table does not contain a brand_id it has a store_id, the StoreProducts has a store_id and product_id and the product table has a id and brand_id.
– Orbitalcoder
Nov 26 '18 at 14:00
I want to filter out the stores with a specific brand, but to find out if the store has the specific brand of product I have to go through two tables and match ids. The store table does not contain a brand_id it has a store_id, the StoreProducts has a store_id and product_id and the product table has a id and brand_id.
– Orbitalcoder
Nov 26 '18 at 14:00
add a comment |
1 Answer
1
active
oldest
votes
I think your relations goes like this:
Stores -> (n*n)StoreProducts -> Products -> (n*1)Brands
which StoreProducts is a joining table between Stores and Products. With this assumption, I would use something like this:
In the Store model,
public function products(){
return $this->belongsToMany(AppProduct::class, "store_products");
}
In the Products model,
public function stores(){
return $this->belongsToMany(AppStore::class, "store_products");
}
In the StoreProducts model,
public function store(){
return $this->belongsTo(AppStore::class);
}
public function product(){
return $this->belongsTo(AppProduct::class);
}
This way the relationship is created. Then you can use something like this:
$stores = AppStore::whereHas("products", function($q) use ($brandId){
return $q->where("brand_id", $brandId);
});
This will give you the stores which the brand exists.
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%2f53482182%2flaravel-left-join-multiple-tables-and-return-the-original-table-value%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
I think your relations goes like this:
Stores -> (n*n)StoreProducts -> Products -> (n*1)Brands
which StoreProducts is a joining table between Stores and Products. With this assumption, I would use something like this:
In the Store model,
public function products(){
return $this->belongsToMany(AppProduct::class, "store_products");
}
In the Products model,
public function stores(){
return $this->belongsToMany(AppStore::class, "store_products");
}
In the StoreProducts model,
public function store(){
return $this->belongsTo(AppStore::class);
}
public function product(){
return $this->belongsTo(AppProduct::class);
}
This way the relationship is created. Then you can use something like this:
$stores = AppStore::whereHas("products", function($q) use ($brandId){
return $q->where("brand_id", $brandId);
});
This will give you the stores which the brand exists.
add a comment |
I think your relations goes like this:
Stores -> (n*n)StoreProducts -> Products -> (n*1)Brands
which StoreProducts is a joining table between Stores and Products. With this assumption, I would use something like this:
In the Store model,
public function products(){
return $this->belongsToMany(AppProduct::class, "store_products");
}
In the Products model,
public function stores(){
return $this->belongsToMany(AppStore::class, "store_products");
}
In the StoreProducts model,
public function store(){
return $this->belongsTo(AppStore::class);
}
public function product(){
return $this->belongsTo(AppProduct::class);
}
This way the relationship is created. Then you can use something like this:
$stores = AppStore::whereHas("products", function($q) use ($brandId){
return $q->where("brand_id", $brandId);
});
This will give you the stores which the brand exists.
add a comment |
I think your relations goes like this:
Stores -> (n*n)StoreProducts -> Products -> (n*1)Brands
which StoreProducts is a joining table between Stores and Products. With this assumption, I would use something like this:
In the Store model,
public function products(){
return $this->belongsToMany(AppProduct::class, "store_products");
}
In the Products model,
public function stores(){
return $this->belongsToMany(AppStore::class, "store_products");
}
In the StoreProducts model,
public function store(){
return $this->belongsTo(AppStore::class);
}
public function product(){
return $this->belongsTo(AppProduct::class);
}
This way the relationship is created. Then you can use something like this:
$stores = AppStore::whereHas("products", function($q) use ($brandId){
return $q->where("brand_id", $brandId);
});
This will give you the stores which the brand exists.
I think your relations goes like this:
Stores -> (n*n)StoreProducts -> Products -> (n*1)Brands
which StoreProducts is a joining table between Stores and Products. With this assumption, I would use something like this:
In the Store model,
public function products(){
return $this->belongsToMany(AppProduct::class, "store_products");
}
In the Products model,
public function stores(){
return $this->belongsToMany(AppStore::class, "store_products");
}
In the StoreProducts model,
public function store(){
return $this->belongsTo(AppStore::class);
}
public function product(){
return $this->belongsTo(AppProduct::class);
}
This way the relationship is created. Then you can use something like this:
$stores = AppStore::whereHas("products", function($q) use ($brandId){
return $q->where("brand_id", $brandId);
});
This will give you the stores which the brand exists.
answered Nov 26 '18 at 14:11
Taha PaksuTaha Paksu
11.6k12857
11.6k12857
add a comment |
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%2f53482182%2flaravel-left-join-multiple-tables-and-return-the-original-table-value%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
Do you want the stores which contains a brand?
– Taha Paksu
Nov 26 '18 at 13:48
I want to filter out the stores with a specific brand, but to find out if the store has the specific brand of product I have to go through two tables and match ids. The store table does not contain a brand_id it has a store_id, the StoreProducts has a store_id and product_id and the product table has a id and brand_id.
– Orbitalcoder
Nov 26 '18 at 14:00