{"id":1327,"date":"2020-11-03T04:57:22","date_gmt":"2020-11-03T04:57:22","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/11\/03\/create-model-file-to-perform-crud-operation-in-magento-2\/"},"modified":"2025-07-24T15:58:55","modified_gmt":"2025-07-24T10:28:55","slug":"create-model-file-to-perform-crud-operation-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-model-file-to-perform-crud-operation-in-magento-2\/","title":{"rendered":"How to Create Model File to Perform CRUD Operation in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the previous tutorial, you came to know\u00a0<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-create-table\/\">how to create a table in Magento 2<\/a>. This tutorial is all about creating a model file to perform CRUD operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use a table in Magento, one has to create files for the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bookmark this tutorial while developing a Magento 2 module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create Model File to Perform CRUD Operation in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file having a name Extension.php in given address:&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\Model<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The name of the table is Extension here so we have given the name of a file&nbsp;<strong>Extension.php.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For fetching and loading the data, you need to create another file \u201cExtension.php\u201d on a given location.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Meetanshi\\Extension\\Model;\n\nuse Magento\\Framework\\Model\\AbstractModel;\nuse Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb;\nclass Extension extends AbstractModel\n{\n    protected function _construct()\n    {\n        $this->_init('Meetanshi\\Extension\\Model\\ResourceModel\\Extension');\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create file&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\Model\\ResourceModel\\Extension.php<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Meetanshi\\Extension\\Model\\ResourceModel;\n\nuse Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb;\nclass Extension extends AbstractDb\n{\n    protected function _construct()\n    {\n        $this->_init('extension', 'id');\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another important file is Collection.php<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collection.php file helps to collect all the data of the table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the given path, create a file and copy the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong><em><strong>app\\code\\Extension\\Model\\ResourceModel\\Extension\\Collection.php<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Meetanshi\\Extension\\Model\\ResourceModel\\Extension;\n\nclass Collection extends \\Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection\n{\n    protected function _construct()\n    {\n        $this->_init('Meetanshi\\Extension\\Model\\Extension', 'Meetanshi\\Extension\\Model\\ResourceModel\\Extension');\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note, For instance, you need to send data from other platforms\u2019 registration forms to the Magento database or integrate API in mobile. For such requirements, we have to call an API that sends a request to the server. In such scenarios, use the code to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/perform-crud-operations-using-api-in-magento-2\/\">perform CRUD operation using API in Magento 2<\/a>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I would be happy to answer your questions regarding this blog.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please do consider sharing this blog with interested ones via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks a bunch.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous tutorial, you came to know\u00a0how to create a table in Magento 2. This tutorial is all about creating a model file to&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1327","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1327"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1327\/revisions"}],"predecessor-version":[{"id":9906,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1327\/revisions\/9906"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}