Created
December 24, 2022 04:53
-
-
Save aklimaruhina/9576556eb4399fcfd0363bd995b5a400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.step one: add following this code to your category model | |
// Each category may have multiple products | |
public function products() | |
{ | |
return $this->belongsToMany(Product::class)->orderBy('id','DESC'); //since its a manytomany relation | |
} | |
2. step two: to show them in your view file | |
first get all category | |
@foreach($categories as $key => $category) | |
@foreach($category->products->where('show_home',1)->where('total_qty', '>', 0)->take('11') as $pKey => $product) | |
................. continue. | |
#laravel_tips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
laravel show products based on category