1

I am trying to fetch table on frontend but getting this error

Fatal error: Class 'Crud\Crudatfrontend\Model\ResourceModel\Post' not found in D:\xampp\htdocs\newmag\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 93

app/code/Crud/Crudatfrontend/
├── Block
│   └── index.php         
├── etc
│    └── module.xml
├── Model
│   ├── Post.php
│   ├── ResourceModel
│   │   ├── Post
│   │   │   └── Collection.php
│   │   └── Post.php
├── registration.php
├── Setup
│   └── InstallSchema.php
└── view
    └── frontend
        ├── layout
        │   └── crud_index_index.xml
        ├── templates
            └── index.phtml

Here is my code,

Block/Index.php

<?php

namespace Crud\Crudatfrontend\Block;

use Magento\Framework\App\Filesystem\DirectoryList;

class Index extends \Magento\Framework\View\Element\Template
{
    protected $_filesystem;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Crud\Crudatfrontend\Model\PostFactory $postFactory
        )
    {           
        parent::__construct($context);
        $this->_postFactory = $postFactory;
    }   

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

Model/Post.php

<?php
namespace Crud\Crudatfrontend\Model;
class Post extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
        protected function _construct()
        {
                $this->_init('Crud\Crudatfrontend\Model\ResourceModel\Post');
        }

        public function getIdentities()
        {
                return [self::CACHE_TAG . '_' . $this->getId()];
        }

        public function getDefaultValues()
        {
                $values = [];

                return $values;
        }
}

Model\ResourceModel\Post.php

<?php

namespace Crud\Crudatfrontend\Model\ResourceModel;

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

class HelloWorld extends AbstractDb
{
    /**
     * Model initialization
     */
    protected function _construct()
    {
        $this->_init('Crud_Crudatfrontend', 'post_id');
    }
}

Model\ResourceModel\Post\collection.php

<?php
namespace Crud\Crudatfrontend\Model\ResourceModel\Post;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
        /**
         * Define resource model
         *
         * @return void
         */
        protected function _construct()
        {
                $this->_init('Crud\Crudatfrontend\Model\Post', 'Crud\Crudatfrontend\Model\ResourceModel\Post');
        }
}
2

Resources class name is wrong

class HelloWorld extends AbstractDb

it should be

class Post extends AbstractDb

Change it to Model\ResourceModel\Post.php

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.