Create Read Update Delete (CRUD) – Magento 2

This tutorial is the continuation of create table tutorial, after creating table, we will create the Model for CRUD (Create Read Update Delete) operation.

I will explain crud operation for the below table structure

For CRUD operation you need to create three things.

1. Model

In Model you need to initialize resource model


2. Resource Model

In Resource model, you need to initialize table name and primary key.


3. Collection

In collection, you need to define Model and Resource Model.


That’s it, now you can able to fetch table value in any block using dependency injection.

Get and Set Data in Block

Please note, we pass Bilal/Cruddemo/Model/CruddemoFactory in constructor, but none of the file found that location.

In Magento 2, each CRUD model has a corresponding factory class. All factory class names are the name of the model class, appended with the word “Factory”. So, since our model class is named Bilal/Cruddemo/Model/Bilal/Cruddemo this means our factory class is named Bilal/Cruddemo/Model/Bilal/CruddemoFactory


after that, you can call this block method in your template file


If you load the URL, one record gets added into our custom table and the page looks like

I hope it helps! Please carry the same source code to follow next tutorial.

About Bilal

I'm a software programmer, I handle Magento2 modifications at any level.

View all posts by Bilal →

3 Comments on “Create Read Update Delete (CRUD) – Magento 2”

  1. where is the routes.xml file and Controller?
    Because i am not able to locate this module in my URL.

    Please tell me, how did you got output on frontend screen without a routes.xml.

    Thank you,
    Mukesh Suthar

    1. Hi Mukesh, Sorry for the inconvenience. It is a pre-assumption tutorial like you know to create frontend stuff. Please first create your basic module using this online generator, it will generate all the required files like module.xml, routes.xml, controller, block, templates. After that follow this blog. If you have any difficulties, feel free to mention in the comment. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *