0

I have created a custom module and I have return collection array but it does not return foreach array in Magento 2.3.2. it returns the only foreach last value.

Block Code

<?php
/**
 * @copyright Devidas
 *
 * @see PROJECT_LICENSE.txt
 */

namespace Developer\Infographics\Block\Index;

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\CategoryFactory;


class Leftinfographics extends Template {


    protected $_resource;
    protected $session;
    /**
     * @param \Magento\Framework\Registry $registry
     */
    private $_registry;
    /**
     * @param \Magento\Framework\Registry $registry
     */
    protected $_catalogSession;
    /**
     * @param \Magento\Catalog\Model\CategoryFactory $categoryfactory
     */
    private $_categoryFactory;
    /**
     * @param Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
     */
    protected $_productCollectionFactory;
    /**
    *@param Magento\Catalog\Model\Category $catgory
    **/
    protected $_category;
    /**
     * Constructor
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Catalog\Model\Session $catalogSession
     **/
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Developer\Infographics\Model\InfographicsFactory $collectionFactory,
        \Magento\Framework\View\Page\Config $pageConfig,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Framework\Session\SessionManagerInterface $session,
        \Magento\Catalog\Model\Category $catgory,
        CategoryFactory $categoryfactory,
        \Magento\Framework\App\ResourceConnection $resource,
        array $data = []) 
    {

        parent::__construct($context, $data);
        $this->pageConfig = $pageConfig;
        $this->collectionFactory = $collectionFactory;
        $this->_registry = $registry;
        $this->_catalogSession = $catalogSession;
        $this->session = $session;
        $this->_categoryFactory = $categoryfactory;
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_category = $catgory;
        $this->_resource = $resource;
    }

    public function getCategoryViceversaCollection()
    {

        $collection = array();
        $finalsearch = 'test,magento,abc,xyz'
        $commaList = explode(',', $finalsearch);
        foreach ($commaList as $key => $value) {
        $collection = $this->collectionFactory->create()->getCollection()->addFieldToFilter('tags', array('like' => '%'.$value.'%'));
        }
        return $collection;


    }




}

Template Code

$productxyzdata = $block->getXYZ();
var_dump($productCollectiondata->getData());
  • this is because you are creating a new collection with every iteration, thus only having the last one – Philipp Sander Nov 6 '19 at 8:48
  • expose your complete code if possible @Devidas – Prathap Gunasekaran Nov 6 '19 at 10:21
  • yes sure........! – Devidas Nov 6 '19 at 10:31
  • Upload your full code please. – Rohan Hapani Nov 6 '19 at 10:33
  • check I have update code @RohanHapani – Devidas Nov 6 '19 at 10:39
0

this is because you are creating a new collection with every iteration, thus only having the last one

$collection = array();
$finalsearch = 'test,magento,abc,xyz'
$commaList = explode(',', $finalsearch);
$collection = $this->collectionFactory->create();
foreach ($commaList as $key => $value) {
    $collection->addFieldToFilter('tags', array('like' => '%'.$value.'%'));
}
return $collection;
| improve this answer | |
  • error invalid method Developer\Infographics\Model\Infographics::addFieldToFilter – Devidas Nov 6 '19 at 9:00
0

Try using this way

$collection = [];
$finalsearch = 'test,magento,abc,xyz';
$commaList = explode(',', $finalsearch);
foreach ($commaList as $key => $value) {
    $collection[] = $this->collectionFactory->create()->getCollection()->addFieldToFilter('tags', array('like' => '%'.$value.'%'));
}
return $collection;
| improve this answer | |
  • No it's not working – Devidas Nov 6 '19 at 9:48
  • What does error occur if you use this? – Dhaduk Mitesh Nov 6 '19 at 10:56
0

Try this below code :

$collection = [];
$finalsearch = 'test,magento,abc,xyz';
$commaList = explode(',', $finalsearch);
foreach ($commaList as $key => $value) {
    $collection[] = $this->collectionFactory->create()->addFieldToFilter('tags', ['like' => '%' . $value . '%'])->getData();
}
return $collection;

make sure your collection class should be Developer\Infographics\Model\ResourceModel\Infographics\CollectionFactory

| improve this answer | |
  • same error invalid method Developer\Infographics\Model\Infographics::addFieldToFilter – Devidas Nov 6 '19 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.