I have created a custom table in database of magento 2. I want to insert some data into this custom table. I want to pass this data from controller to the custom table in magento 2.4. Please can any one guide how can i do that.

3 Answers 3


You need to create models

=>Resource Model ( for connect with database)

=>Model ( get and save data)


You can get reference from Devdoc or




You can insert data into database using one of the below two methods

Method 1 : https://bsscommerce.com/confluence/how-to-create-insert-data-into-the-table-in-magento-2/

Method 2 :

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $tableName = $resource->getTableName('tbl_name'); //gives table name with prefix

    $sql = "Insert Into " . $tableName . " (id, name) Values ('','XYZ')";

namespace YourNameSpace\YourModule\Helper;

use Magento\Framework\App\ResourceConnection;

class Data extends AbstractHelper
    private $connection;

    public function __construct(
        ResourceConnection $resourceConnection
        $this->connection = $resourceConnection->getConnection();

    public function saveData()
        $bind = [
            'column1' => $column1,
            'column2' => $column2,
            'column3' => $column3,
            'column4' => $column14,
        $this->connection->insert('your_table_name', $bind);

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.