0

I have a table name my_cars. There are 4 column in my table. I create collection, model, recourse model,controller and block. And i can display data from my table. But all column of data not displaying. enter image description here

car Id , Manufacturer and created at are displaying in phtml. but Model are not displaying. enter image description here

My Model\Hello.php:

<?php

namespace Codilar\Helloworld\Model;

use Magento\Framework\Model\AbstractModel;

class Hello extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Codilar\Helloworld\Model\ResourceModel\Hello');
    }

} 

My ResourceModel\Hello.php

<?php

namespace Codilar\Helloworld\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Hello extends AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('my_cars', 'car_id '); //my_cars is table of module
    }
}

My ResourceModel\Hello\collection.php

<?php

namespace Codilar\Helloworld\Model\ResourceModel\Hello;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Codilar\Helloworld\Model\Hello',
            'Codilar\Helloworld\Model\ResourceModel\Hello'
        );
    }
}

My Block\Hello.php

<?php
/**
 *
 * @package     magento2
 * @author      Codilar Technologies
 * @license     https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
 * @link        https://www.codilar.com/
 */

namespace Codilar\HelloWorld\Block;


use Magento\Framework\App\Filesystem\DirectoryList;
class Hello extends \Magento\Framework\View\Element\Template
{
     protected $_filesystem;
 
     public function __construct(
          \Magento\Framework\View\Element\Template\Context $context,
          \Codilar\HelloWorld\Model\HelloFactory $HelloFactory
          )
     {
          parent::__construct($context);
          $this->_HelloFactory = $HelloFactory;
     }
 
     public function getResult()
     {
          $post = $this->_HelloFactory->create();
          $collection = $post->getCollection();
          return $collection;
     }
}

My Controller\Index\Index.php

<?php

namespace Codilar\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Codilar\Helloworld\Model\HelloFactory;


class Index extends \Magento\Framework\App\Action\Action
{
     protected $_pageFactory;
 
     public function __construct(
          \Magento\Framework\App\Action\Context $context,
          \Magento\Framework\View\Result\PageFactory $pageFactory
          )
     {
          $this->_pageFactory = $pageFactory;
          return parent::__construct($context);
     }
 
     public function execute()
     {
        return $this->_pageFactory->create();
        //   echo "Module Created Successfully";
     }
}

Not Only for this table but also i checked another table its just display ID,Name,Created at ,updated at. without those 4 column there is not data displaying in view page. My view\frontend\layout\helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Codilar\HelloWorld\Block\Hello" name="helloworld" template="Codilar_HelloWorld::hello.phtml" />
        </referenceContainer>
    </body>
</page>

My view\frontend\templates\Hello.phtml

<table>
     <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Model</th>
          <th>Created At</th>
          <th colspan="2">Action</th>
     </tr>
 
     <?php
          $collection = $block->getResult();
          foreach($collection as $item){
     ?>
     <tr>
          <td><?php echo $item->getData('car_id'); ?></td>
          <td><?php echo $item->getData('manufacturer'); ?></td>
          <td><?php echo $item->getDate('model'); ?></td>
     
          <td><?php echo $item->getData('created_at'); ?></td>
          <td><a href="<?php echo $block->getUrl('crud/index/edit').'id/'.$item->getData('post_id'); ?>" class="action primary">Edit</a></td>
          <td><a href="<?php echo $block->getUrl('crud/index/delete').'?id='.$item->getData('post_id'); ?>" class="action primary">Delete</a></td>
     </tr>
     <?php } ?>
</table>

Can anyone give any solution for This problem? If can i will be glad for this. Thank you.

1 Answer 1

0

It's a typo by your side at this line <td><?php echo $item->getData('model'); ?></td>. Hope, it's gonna work now.You wrote getDate instead of getData. :)

Your Answer

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

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