Skip to content

Instantly share code, notes, and snippets.

@aklimaruhina
Created December 24, 2022 04:53
Show Gist options
  • Save aklimaruhina/9576556eb4399fcfd0363bd995b5a400 to your computer and use it in GitHub Desktop.
Save aklimaruhina/9576556eb4399fcfd0363bd995b5a400 to your computer and use it in GitHub Desktop.
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
@aklimaruhina
Copy link
Author

laravel show products based on category

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment