Laravel - Many-to-many relation, product count by product featureDjango - querying objects via many-to-many relationFor each result return by SQL query I want as to give a different nameSQL filter from many-to-many relationshipLaravel count categories joinLaravel Eloquent: how to filter reviews of products by product category and/or product brand?How to count how many articles belongs which category?found error in laravel 5.4,Property [categories] does not exist on this collection instanceCounting Rows based on Identifier - LaravelHow to make filter products on laravel correctly?Laravel Model Count By Relationship
How is it believable that Euron could so easily pull off this ambush?
My large rocket is still flipping over
In the figure, a quarter circle, a semicircle and a circle are mutually tangent inside a square of side length 2. Find the radius of the circle.
Function annotation with two or more return parameters
Drug Testing and Prescribed Medications
Select list elements based on other list
Is there a reason why Turkey took the Balkan territories of the Ottoman Empire, instead of Greece or another of the Balkan states?
How does jetBlue determine its boarding order?
Splitting polygons and dividing attribute value proportionally using ArcGIS Pro?
Convert a huge txt-file into a dataset
Can anyone identify this unknown 1988 PC card from The Palantir Corporation?
Appropriate age to involve kids in life changing decisions
If an attacker targets a creature with the Sanctuary spell cast on them, but fails the Wisdom save, can they choose not to attack anyone else?
How can I finally understand the confusing modal verb "мочь"?
Gift for mentor after his thesis defense?
In a series of books, what happens after the coming of age?
Do the Zhentarim fire members for killing fellow members?
Translation of "invincible independence"
What does the copyright in a dissertation protect exactly?
How could a humanoid creature completely form within the span of 24 hours?
How to increase speed on my hybrid bike with flat handlebars and 700X35C tyres?
My parents are Afghan
Why doesn't increasing the temperature of something like wood or paper set them on fire?
Why is the blank symbol not considered part of the input alphabet of a Turing machine?
Laravel - Many-to-many relation, product count by product feature
Django - querying objects via many-to-many relationFor each result return by SQL query I want as to give a different nameSQL filter from many-to-many relationshipLaravel count categories joinLaravel Eloquent: how to filter reviews of products by product category and/or product brand?How to count how many articles belongs which category?found error in laravel 5.4,Property [categories] does not exist on this collection instanceCounting Rows based on Identifier - LaravelHow to make filter products on laravel correctly?Laravel Model Count By Relationship
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Trying to get the count of products by its features(many-to-many)
For instance,
Product::with([’features’])->where(category_id, $category_id)->get();
Product A->feature1->id
/feature2->id
/feature3->id
Product B->feature3->id
/feature4->id
/feature6->id
.....
how could i get product count from each feature (after filtered by product category)
I am not good at explain in wording, tried my best.
Final result
feature 1 -> 19 products
feature 2 -> 5 products
...
php laravel eloquent many-to-many
add a comment |
Trying to get the count of products by its features(many-to-many)
For instance,
Product::with([’features’])->where(category_id, $category_id)->get();
Product A->feature1->id
/feature2->id
/feature3->id
Product B->feature3->id
/feature4->id
/feature6->id
.....
how could i get product count from each feature (after filtered by product category)
I am not good at explain in wording, tried my best.
Final result
feature 1 -> 19 products
feature 2 -> 5 products
...
php laravel eloquent many-to-many
add a comment |
Trying to get the count of products by its features(many-to-many)
For instance,
Product::with([’features’])->where(category_id, $category_id)->get();
Product A->feature1->id
/feature2->id
/feature3->id
Product B->feature3->id
/feature4->id
/feature6->id
.....
how could i get product count from each feature (after filtered by product category)
I am not good at explain in wording, tried my best.
Final result
feature 1 -> 19 products
feature 2 -> 5 products
...
php laravel eloquent many-to-many
Trying to get the count of products by its features(many-to-many)
For instance,
Product::with([’features’])->where(category_id, $category_id)->get();
Product A->feature1->id
/feature2->id
/feature3->id
Product B->feature3->id
/feature4->id
/feature6->id
.....
how could i get product count from each feature (after filtered by product category)
I am not good at explain in wording, tried my best.
Final result
feature 1 -> 19 products
feature 2 -> 5 products
...
php laravel eloquent many-to-many
php laravel eloquent many-to-many
asked Mar 23 at 5:49
Hao PhungHao Phung
397
397
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Try this:
Select product_id from products where category_id = "'.$category_id.'" group by feature_id;
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
add a comment |
You should Define relation within your Product
and Feature
Model
class Product extends Model
public function features()
return $this->belongsTo(Feature::class);
class Feature extends Model
public function products()
return $this->hasMany(Product::class);
I presume each Product
has an attribute feature_id
which hold the id of the Feature
to which it's belongsTo.
$products = Product::with([’features’])->where(category_id, $category_id)->get(); // This query will return list of product
foreach($products as $product)
// You can access $feature of product like this
$feature = $product->feature;
Because I have already define the reverse relation between the both Model I can access Product from Feature as well.
$feature->products(); // This will return a collection of Product and I can perform any sort of query on that too
// Like count number of Products
$feature->products()->count();
$feature->products()->first(); // get the first product
$feature->products()->last(); // get the last product
And so on and so fourth
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
add a comment |
Assuming you have products
relationship on the Feature
model you can try this!
$features = Feature::withCount(['products' => function($query)
$query->where('category_id', $category_id);
])->get();
You will have a products_count
with each record of the collection.
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
add a comment |
This will do the trick
with(['products' => function($q) use($category_id)
$q->where('category_id',18);
])
->find($feature_ids)
->groupBy('feature_type_id');
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
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%2f55311014%2flaravel-many-to-many-relation-product-count-by-product-feature%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
Select product_id from products where category_id = "'.$category_id.'" group by feature_id;
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
add a comment |
Try this:
Select product_id from products where category_id = "'.$category_id.'" group by feature_id;
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
add a comment |
Try this:
Select product_id from products where category_id = "'.$category_id.'" group by feature_id;
Try this:
Select product_id from products where category_id = "'.$category_id.'" group by feature_id;
answered Mar 23 at 5:57
Korat PrakashKorat Prakash
1315
1315
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
add a comment |
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
This is a good idea, i will try after i got home
– Hao Phung
Mar 23 at 6:11
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
How could I groupBy feature->id? in product collection each product might have couple feature collections, so product->features->feature1->id, product->features->feature2->id, product->features->feature3->id
– Hao Phung
Mar 23 at 11:20
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
$product_features_with_count = Feature:: with(['products']) // ->where('products.category_id', $id) ->whereHas('products', function($q) use($id) return $q->where('products.category_id', $id); ) ->find($feature_ids) ->groupBy('feature_type_id');
– Hao Phung
Mar 23 at 16:27
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
I am almost there, but not collection doesn't query the category, it includes everything
– Hao Phung
Mar 23 at 16:28
add a comment |
You should Define relation within your Product
and Feature
Model
class Product extends Model
public function features()
return $this->belongsTo(Feature::class);
class Feature extends Model
public function products()
return $this->hasMany(Product::class);
I presume each Product
has an attribute feature_id
which hold the id of the Feature
to which it's belongsTo.
$products = Product::with([’features’])->where(category_id, $category_id)->get(); // This query will return list of product
foreach($products as $product)
// You can access $feature of product like this
$feature = $product->feature;
Because I have already define the reverse relation between the both Model I can access Product from Feature as well.
$feature->products(); // This will return a collection of Product and I can perform any sort of query on that too
// Like count number of Products
$feature->products()->count();
$feature->products()->first(); // get the first product
$feature->products()->last(); // get the last product
And so on and so fourth
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
add a comment |
You should Define relation within your Product
and Feature
Model
class Product extends Model
public function features()
return $this->belongsTo(Feature::class);
class Feature extends Model
public function products()
return $this->hasMany(Product::class);
I presume each Product
has an attribute feature_id
which hold the id of the Feature
to which it's belongsTo.
$products = Product::with([’features’])->where(category_id, $category_id)->get(); // This query will return list of product
foreach($products as $product)
// You can access $feature of product like this
$feature = $product->feature;
Because I have already define the reverse relation between the both Model I can access Product from Feature as well.
$feature->products(); // This will return a collection of Product and I can perform any sort of query on that too
// Like count number of Products
$feature->products()->count();
$feature->products()->first(); // get the first product
$feature->products()->last(); // get the last product
And so on and so fourth
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
add a comment |
You should Define relation within your Product
and Feature
Model
class Product extends Model
public function features()
return $this->belongsTo(Feature::class);
class Feature extends Model
public function products()
return $this->hasMany(Product::class);
I presume each Product
has an attribute feature_id
which hold the id of the Feature
to which it's belongsTo.
$products = Product::with([’features’])->where(category_id, $category_id)->get(); // This query will return list of product
foreach($products as $product)
// You can access $feature of product like this
$feature = $product->feature;
Because I have already define the reverse relation between the both Model I can access Product from Feature as well.
$feature->products(); // This will return a collection of Product and I can perform any sort of query on that too
// Like count number of Products
$feature->products()->count();
$feature->products()->first(); // get the first product
$feature->products()->last(); // get the last product
And so on and so fourth
You should Define relation within your Product
and Feature
Model
class Product extends Model
public function features()
return $this->belongsTo(Feature::class);
class Feature extends Model
public function products()
return $this->hasMany(Product::class);
I presume each Product
has an attribute feature_id
which hold the id of the Feature
to which it's belongsTo.
$products = Product::with([’features’])->where(category_id, $category_id)->get(); // This query will return list of product
foreach($products as $product)
// You can access $feature of product like this
$feature = $product->feature;
Because I have already define the reverse relation between the both Model I can access Product from Feature as well.
$feature->products(); // This will return a collection of Product and I can perform any sort of query on that too
// Like count number of Products
$feature->products()->count();
$feature->products()->first(); // get the first product
$feature->products()->last(); // get the last product
And so on and so fourth
answered Mar 23 at 6:38
Yves KipondoYves Kipondo
1,563715
1,563715
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
add a comment |
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
They have been defined, and i also have productFeature model. the collection of products has been filtered by category and also some features too, so the productFeature model seems doesn't help
– Hao Phung
Mar 23 at 11:12
add a comment |
Assuming you have products
relationship on the Feature
model you can try this!
$features = Feature::withCount(['products' => function($query)
$query->where('category_id', $category_id);
])->get();
You will have a products_count
with each record of the collection.
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
add a comment |
Assuming you have products
relationship on the Feature
model you can try this!
$features = Feature::withCount(['products' => function($query)
$query->where('category_id', $category_id);
])->get();
You will have a products_count
with each record of the collection.
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
add a comment |
Assuming you have products
relationship on the Feature
model you can try this!
$features = Feature::withCount(['products' => function($query)
$query->where('category_id', $category_id);
])->get();
You will have a products_count
with each record of the collection.
Assuming you have products
relationship on the Feature
model you can try this!
$features = Feature::withCount(['products' => function($query)
$query->where('category_id', $category_id);
])->get();
You will have a products_count
with each record of the collection.
edited Mar 23 at 17:17
answered Mar 23 at 6:58
Haider AliHaider Ali
681618
681618
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
add a comment |
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
The category_id belongs to products, not from feature, so where clause doesn't work
– Hao Phung
Mar 23 at 16:48
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
@HaoPhung i have updated my answer please try it!
– Haider Ali
Mar 23 at 17:17
add a comment |
This will do the trick
with(['products' => function($q) use($category_id)
$q->where('category_id',18);
])
->find($feature_ids)
->groupBy('feature_type_id');
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
add a comment |
This will do the trick
with(['products' => function($q) use($category_id)
$q->where('category_id',18);
])
->find($feature_ids)
->groupBy('feature_type_id');
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
add a comment |
This will do the trick
with(['products' => function($q) use($category_id)
$q->where('category_id',18);
])
->find($feature_ids)
->groupBy('feature_type_id');
This will do the trick
with(['products' => function($q) use($category_id)
$q->where('category_id',18);
])
->find($feature_ids)
->groupBy('feature_type_id');
answered Mar 23 at 18:06
Hao PhungHao Phung
397
397
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
add a comment |
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
Please mark the answer as accepted. This shows other users that the issue is resolved.
– Jonas Staudenmeir
Mar 29 at 22:05
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%2f55311014%2flaravel-many-to-many-relation-product-count-by-product-feature%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