0
<?php

namespace WebQillaModule\FinalModule\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{  
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();


        $setup->getConnection()
            ->addColumn(
                $setup->getTable('first_table'),
                'Date',
                 \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
                null,
                'Post Date'
            );

        $setup->getConnection()
            ->addColumn(
                $setup->getTable('first_table'),
                'Logo',
                 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                null,
                'Post Logo'
        );

        $setup->endSetup();

    }
}

I added these new 2 new fields in my table first table how to show these in front end page of this custom module

front page showing only title and content of blog post of my custom module

enter image description here

0

Your UpgradeSchema.php is wrong. please check here.

<?php

namespace WebQillaModule\FinalModule\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $tableName = $setup->getTable('first_table');
        // you must check you module version in module.xml (must be same)
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $setup->getConnection()->addColumn(
                $setup->getTable($tableName),
                'logo',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => false,
                    'comment' => 'Post Logo',
                ]
            );
        }

        $setup->endSetup();
    }
}

And you module.xml file in app/code/WebQillaModule/FinalModule/etc has same vesion name like below

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="WebQillaModule_FinalModule" setup_version="1.0.1">
    </module>
</config>

After save both file run this command

php bin/magento s:up
php bin/magento s:s:d
php bin/magento c:c

You can check version is update or not in setup_module table. and also check this column is added in you table or not. and you can get this column value like other like other column value.

Update

create an Modal

app/code/WebQillaModule/FinalModule/Model

Customtable.php

<?php

namespace WebQillaModule\FinalModule\Model;

use WebQillaModule\FinalModule\Model\ResourceModel\Customtable as CustomtableResourceModel;

class Customtable extends \Magento\Framework\Model\AbstractModel
{
    const CACHE_TAG = 'first_table';

    protected $_cacheTag = 'first_table';
    protected $_eventPrefix = 'first_table';

    protected function _construct()
    {
        $this->_init(CustomtableResourceModel::class);
    }

}

app/code/WebQillaModule/FinalModule/Model/ResourceModel

Customtable.php

<?php

namespace WebQillaModule\FinalModule\Model\ResourceModel;

class Customtable extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected $_idFieldName = 'id';
    protected $_date;

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        $resourcePrefix = null
    ) {
        parent::__construct($context, $resourcePrefix);
        $this->_date = $date;
    }
    protected function _construct()
    {
        $this->_init('first_table', 'id'); // add here your primary key column name
    }
}

app/code/WebQillaModule/FinalModule/Model/ResourceModel/Customtable

Collection.php

<?php

namespace WebQillaModule\FinalModule\Model\ResourceModel\Customtable;

use WebQillaModule\FinalModule\Model\Customtable as CustomtableModel;
use WebQillaModule\FinalModule\Model\ResourceModel\Customtable as CustomtableResourceModel;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected $_idFieldName = 'id';

    protected function _construct()
    {
        $this->_init(
            CustomtableModel::class,
            CustomtableResourceModel::class
        );
    }
}

create controller file

app/code/WebQillaModule/FinalModule/Controller/Index

Index.php

<?php

namespace WebQillaModule\FinalModule\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
    public function execute()
    {

        return $this->resultPageFactory->create();
    }
}

crate an block file

app/code/WebQillaModule/FinalModule/Block

CustomeBlock.php

<?php

namespace WebQillaModule\FinalModule\Block;

use Magento\Framework\App\Request\DataPersistorInterface;

class CustomeBlock extends \Magento\Framework\View\Element\Template
{
    protected $customData;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \WebQillaModule\FinalModule\Model\CustomtableFactory $customData
    ) {
        parent::__construct($context);
        $this->customData = $customData;
    }
    public function getCustomTableData()
    {
        return $this->customData->create();
    }
}

add this into phtml file

app/code/WebQillaModule/FinalModule/view/frontend/templates

customefile.phtml

<?php
echo "<pre>";
print_r($block->getCustomTableData()->getData());
echo "</pre>";
?>
<h1> Call phtml file</h1>

app/code/WebQillaModule/FinalModule/view/frontend/layout

tempfront_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="WebQillaModule\FinalModule\Block\CustomeBlock" template="WebQillaModule_FinalModule::customefile.phtml"/>
        </referenceContainer>
    </body>
</page>

after that run magento command

php bin/magento s:up
php bin/magento s:s:d
php bin/magento c:c

Note: please check your custome table in database and also check your Module name in setup_module table.

I Hope This Helps You.

| improve this answer | |
  • for display your data check this link magento.stackexchange.com/questions/303727/… – Msquare Apr 3 at 7:05
  • Thank you so much for your reply but this is not what i was asking you i have done this code and got table output correctly title,content, date and image is saving in database table i just want to show this fields (date and image) in frontend too like title and content – Fareeha Rana Apr 3 at 10:31
  • Check my Updated answer and update me. – Msquare Apr 3 at 10:42
  • I want to clear one thing i have a model and controller for title and content do i need to create separate model and controller for showing new fields? and please see my above updated question too – Fareeha Rana Apr 3 at 11:48
  • Can I know about what you want to do.you can call data sa your requirement. – Msquare Apr 3 at 12:35
0

You can call your table data on frontend, you can add below code on block and call block function in template.

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Vendor\Modulename\Model\ModelnameFactory $modelnameFactory,        
    array $data = []
)
{    
    $this->modelnameFactory= $modelnameFactory;    
    parent::__construct($context, $data);
}

 public function getCollection()
{
    $collection = $this->modelnameFactory->create()->getCollection();
    return $collection;
}

You need to add below code to your template

$collection = $block->getCollection();
| improve this answer | |
  • when you say block, are you talking about the vendor\module\Block\Adminhtml ? – Yuri Rodrigues Apr 11 at 4:51

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.