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
namespace Developer\Infographics\Block\Index;

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


class Leftinfographics extends Template {

    public function __construct(
   \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,

        array $data = []) 
    {

        parent::__construct($context, $data);
        $this->pageConfig = $pageConfig;
        $this->collectionFactory = $collectionFactory;
    }

    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());
11
  • this is because you are creating a new collection with every iteration, thus only having the last one Commented Nov 6, 2019 at 8:48
  • expose your complete code if possible @Devidas Commented Nov 6, 2019 at 10:21
  • yes sure........! Commented Nov 6, 2019 at 10:31
  • Upload your full code please. Commented Nov 6, 2019 at 10:33
  • check I have update code @RohanHapani Commented Nov 6, 2019 at 10:39

3 Answers 3

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;
1
  • error invalid method Developer\Infographics\Model\Infographics::addFieldToFilter Commented Nov 6, 2019 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;
2
  • No it's not working Commented Nov 6, 2019 at 9:48
  • What does error occur if you use this? Commented Nov 6, 2019 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

1
  • same error invalid method Developer\Infographics\Model\Infographics::addFieldToFilter Commented Nov 6, 2019 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.