I have created a custom module in backend for uploading image slider in homepage, but I am not able to call in homepage?(frontend). I don't know where I am making mistake.

Here is my code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$modelCollection = $objectManager->get('Amy\Homebanner\Model\Grid')->getCollection();
 echo "<pre>";
  echo "</pre>";
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.

<div class="ves-slideshow container">
<div id="owl-demo2" class="owl-carousel owl-theme">
<div class="item">

    <div class="ves-slider">

     <?php foreach ($modelCollection as $_model) { ?>
        <?php if ($_model->getIsActive() == '0' && $_model->getPageSelection() == '1') { ?>
                <li <?php if ($_model->getUrl() != '') { ?> onclick="location.href = '<?php echo $_model->getUrl() ?>'" <?php } ?>>

        <?php if ($deviceDetect->isMobile()) { ?>

                <img src="<?php echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $_model->getImageMobile() ?>" alt="<?php echo $_model['alternate_text'] ?>" title="<?php echo $_model['alternate_text'] ?>" />

        <?php }else{ ?>

                <img src="<?php echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $_model->getImage() ?>" alt="<?php echo $_model['alternate_text'] ?>" title="<?php echo $_model['alternate_text'] ?>" />
        <?php } ?>
        <?php } ?>
        <?php } ?>

When the status is enabled the homepage shows blank, while the status is disabled the homepage is showing but the slider is not showing.

Any help would be appreciated.

  • Nope, it is not module status. It is each field status $_model->getIsActive() == '0'
    – Amy
    Mar 19 '19 at 12:00
  • Do you have helper? if so remove object manager and get the collection by applying filter using DI Mar 19 '19 at 12:01
  • How did you added this phtml to home page?? via xml or cms? Mar 19 '19 at 12:01
  • I am using theme. so i override the theme and applied in theme homepage.
    – Amy
    Mar 19 '19 at 12:03
  • Okay i will try it, and let you know..
    – Amy
    Mar 19 '19 at 12:22

There are quite a few issues with your code

  1. You Should not use object manager directly in you files.
  2. You should not call model directly for collection than create a Repository class for you module , create a getlist Method and filter collection using search criteria .
  3. Rather than getting the full collection filter the collection before by checking IsActive == 1
  4. Use ViewModel rather than posting code in you PHTML

    Regarding the above code issue try

   $modelCollection = $objectManager->get('Amy\Homebanner\Model\Grid');
    $modelCollection ->getCollection()
        ->addFieldToFilter('is_active', '1');
    if ($modelCollection->getSize() > 0) {//show slider?>

if you are still facing the issue switch to developer mode and in file '\app\bootstrap.php' uncomment the following line

#ini_set('display_errors', 1);

Your error will be displayed


Get collection using helper Data.php

Place Data.php in the below path


then add the below code to get the collection by filter i.e., is_active = '0' and page_selection = '1' so

namespace Amy\Homebanner\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
    protected $_scopeConfig;
    public function __construct(
     \Amy\Homebanner\Model\GridFactory $collection
    ) {
       $this->collection = $collection;

    public function getCollection()
       $dataCollection = $this->collection->create()->getCollection()->addFieldToSelect('*')->addFieldToFilter('is_active', array(
        'eq' => '0'
    ))->addFieldToFilter('page_selection', array(
        'eq' => '1'
       return $dataCollection;

then call this in your phtml

$data = $this->helper('Amy\Homebanner\Helper\Data')->getCollection();

then foreach to print the collection as you require

foreach($data as $items){
   echo $items->getIsActive();

NOTE : Object Manager shouldn't used anywhere since because it will create object each time when the file load. So it consume loads of memory and time. Site will literally slow down. Best practice to go for DI.

In addition to above answer, enable developer mode check the issue.

php bin/magento deploy:mode:set developer

Hope it helps .

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.