PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 10, 2022

[FIXED] how to display 15 product from category having multiple subcategories in random order in laravel?

 February 10, 2022     laravel, laravel-5, laravel-6, laravel-7, laravel-8     No comments   

Issue

I have 3 tables:

homepage
    order- integer
    status - boolean
    product_category_id - integer

category
    name- string
    slug- slug
    parent_id- integer

product
    name- string
    category_id- integer

When user wish to have categories in homepage like this enter image description here

I want to have 15 products from categories in homepage including products from subcategories in random order. So far what i have done is

 //  all categories to be posted in homepage.
        $hp_categories = HomepageCategory::asc()->active()->get();

        foreach ($hp_categories as $hp_cat) {
            // array declaration
            $cat_with_subcat_arr = [];
            // category
            $product_category = ProductCategory::where('id', $hp_cat->product_category_id)->first();            
            // push category's id to array
            array_push($cat_with_subcat_arr, $product_category->id);
            // subcategories if any.
            $product_category->childrenCategoriesIds($cat_with_subcat_arr);
            
            // product from categories and subcategories via whereIn.
            $products = Product::whereIN('product_category_id', $cat_with_subcat_arr)->inRandomOrder()->limit(15)->get();
                    
            dd($products);
        }
        return $hp_categories;

my output is like this enter image description here

Note: I could not do it by eager loading. So, I did by simple logic but cannot pass data to view.


Solution

in controller i did

//define a collection;
$hp_cats = collect();

foreach ($hp_categories as $key=>$hp_cat) {

    // array declaration
    $cat_with_subcat_arr = [];

    // category
    $product_category = ProductCategory::where('id', $hp_cat->product_category_id)->first();            
 
    // push category's id to array
            
    array_push($cat_with_subcat_arr, $product_category->id);
            
    // subcategories if any.
            
    $product_category->childrenCategoriesIds($cat_with_subcat_arr);
            
            
    // product from categories and subcategories via whereIn.
            
    $products = Product::whereIN('product_category_id', $cat_with_subcat_arr)
            ->inRandomOrder()
            ->limit(15)
            ->get();
            
            
    $hp_cats[$key] = $products;
        
}

the result enter image description here

Note: I could not found answer so it may not be the best solution. any help is appriciated.



Answered By - mukesh rai
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing