Laravel paginate items in multiple categories - laravel

I have 2 tables, products and categories. Products belong to different categories, and categories are formed in tree structure that allows for parent category and child category relationship. So given a specific category, how should I write query to get paginated products that belong to this category and all its child categories?
I forgot to mention that in my settings, products and categories are many to many relationship, there is a pivoting table named product_catrgory.

$ids = $category->children()->lists('id');
$ids[] = $category->id;
$products = Product::whereIn('category_id', $ids)->paginate(20);

Maybe there is other better method but you could do it this way:
$ids = [];
$ids[] = $category->id;
foreach ($category->children as $child) {
$ids[] = $child->id;
}
$products = Product::whereIn('cat_id', $ids)->paginate(15);

In my opinion the simplest way to do this is to first get category id and its all child category id recursively. And query the database using whereIn(array of category id) to get all the products. I do not think you can do it in one single query.

Related

products with categories using category_products

I have a Relation of products with categories using category_products as a pivot table. I want to get/take 5 products from each category but it not working correctly.
I have tried all the methods known to me I could find the solution
$query = $this->getQuery();
$query->where('parent_id','0')->with('categoryProducts',function($q1){
$q1->with('product',function ($q){
$q->orderBy('sold', 'desc');
})->take(4);
})->whereHas('categoryProducts');
return $query->get();

Laravel 5 - How to Query if DB Value is Array

In database, I have "categories" and "products" table. In "products" table, there is a category_ids column which is an array, because the product has more than one categories.
What I want is to query the product that has category_ids of 1. I am trying to do like this, it is works, but I want a simpler way how to do.
$catId = 1; //category ID
$allProducts = Product::all(); // get all products
foreach ($allProducts as $product) { //loop all the products
if(in_array($catId, $product->category_ids)) { // check if category ID is in array of its product categories
$products[] = Product::find($product->id);
}
}
As you can see above,I must loop through all the products to check if category ID is exist in product categories array.
Is there a better way so it is not checking all the products?
All code is OK except one line .
$catId = 1; //category ID
$allProducts = Product::all(); // get all products
foreach ($allProducts as $product) { //loop all the products
$cats = explode(',',$product->category_ids);
if(in_array($catId, $cats)) { // check if category ID is in array of its product categories
$products[] = Product::find($product->id);
}
}
I added this line. I assume that multiple categories of a product will be stored in a single field with comma separated (Although its not the correct way, you need to create a M2M table)
$cats = explode(',',$product->category_ids);
So In in_array function, 2nd element should be an array. and Because of output of explode, $cats is an array.
in_array($catId, $cats)

Magento - assign multiple product ids to a category

I have a category "new" that I want to assign to, via a daily cron, the most recent 120 products.
I am trying to override the product ids assigned to a category.
Is there an easy way , like :
$category->setProductIds(array())
In PHP code you can put them into the category while you are importing them.
Say you have a product called $product and a category ID called $category_id
You can set the categories which a product belongs to by doing the following
$categories = array($category_id);
$product->setCategoryIds($categories);
$product->save();
f the product already has categories and you'd like to add one more then you can use getCategoryIds() like this:
$categories = $product->getCategoryIds();
$categories[] = $categoryId;
$product->setCategoryIds($categories);
$product->save();
it works like this
$category = Mage::getModel('catalog/category')->load($categoryID);
$product_array = $category->getProductsPosition()
... make changes ... format : $item[$product_id] => $position_in_category
$category->setPostedProducts($product_array);
$category->save();
To Remove product from category:
Mage::getSingleton('catalog/category_api')->removeProduct($category->getId(),$p‌​roduct->getId());
To Add Product to Category:
Mage::getSingleton('catalog/category_api')->assignProduct($category->getId(),$p‌​roduct->getId());
Now you can loop through all products and assign those to category using category id.
Note: This will not overwrite any categories the product is already in.

Magento: How do I get a list of categories from a product collection?

On a certain pages I have a large product collection with products of multiple categories. How do I get a list of categories that are included in the currently loaded product collection?
I can't think of another way other than iterating over the collection and keeping track of the categories of each product. Something like this:
$categories = [];
foreach ($productCollection as $product) {
$categories[] = $product->getCategory();
}
// To get rid of the duplicates
$categories = array_unique($categories);
This way you get the individual product categories, and you can mount the array as it sees best
<?php
$categoryIds = $_product->getCategoryIds();
foreach($categoryIds as $catId){
$category = Mage::getModel('catalog/category')->load($catId);
}
?>

getStoreCategories() to get categories by ids

I would like to get an array of categories for my magento store.
I need to use following unit for my website to work:
$categories = $helper->getStoreCategories('name', true, true);
However this lists all categories.
I would like to select categories that are important for me. I think I could do that by selecting ids of the categories or names of the categories but I don't know how to do it.
Could anybody help me please?
Let's say you have an array with category ids like this:
$ids = array(6,8,99);
You can get the category objects like this:
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $ids);
if you want only active categories add this line also
$collection->addAttributeToFilter('is_active', 1);
Use this function and just pass category Id:
function getCategoryData($_categoryId=null) {
// For category Collection
$category = Mage::getModel('catalog/category')->load($_categoryId);
// For product Collection category wise
$prodCollection = $category->getProductCollection();
return $prodCollection;
}

Resources