Laravel 5 - How to Query if DB Value is Array - laravel-5

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)

Related

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

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
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
Note: I could not do it by eager loading. So, I did by simple logic but cannot pass data to view.
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
Note: I could not found answer so it may not be the best solution. any help is appriciated.

Sort order by Name collection.php (magento)

I have a custom module with change the default CATALOG/RESOURCE/PRODUCT/COMPARE/ITEM/collection.php, but the line below:
->order('ai.sort_order ASC');
Change the order for position with attributes position, but a few of is (about 10 attributes) is just a text (not Dropdown, Select or Price) and cannot allow to orders correctly position iquals a product page order. (i need to leave the page to compare products in the same order from the product page of the attribute list)
how i can make this?
To create sort by position write below code after getting product collection in app/design/frontend/<theme_name>/default/template/catalog/product/list.phtml
$_productCollection = new Varien_Data_Collection();
$sortedCollection = array();
foreach ($_defaultProductCollection as $key => $_product) {
if(!isset($sortedCollection[$positions[$_product->getId()]])){
$sortedCollection[$positions[$_product->getId()]] = array();
}
$sortedCollection[$positions[$_product->getId()]][] = $_product;
}
ksort($sortedCollection);
foreach ($sortedCollection as $_products) {
foreach ($_products as $_product) {
$_productCollection->addItem($_product);
}
}
Hope it will work for you.

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);
}
?>

Laravel paginate items in multiple categories

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.

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