How to create custom database table and Show table data on custom page in Drupal 10

student_records/  
├── student_records.info.yml  
├── student_records.routing.yml  
├── student_records.install  
└── src/  
    └── Controller/  
        └── StudentRecordsController.php
mkdir -p modules/custom/student_records
name: 'Student Records'  
type: module
description: 'Displays student records from a custom database table in the admin panel.'
core_version_requirement: ^10
package: Custom
dependencies:  
  - drupal:system


Create a Custom Route

student_records.routing.yml

student_records.admin_page:  
  path: '/admin/student-records'  
  defaults:  
    _controller: '\Drupal\student_records\Controller\StudentRecordsController::content'  
    _title: 'Student Records'  
  requirements:  
    _permission: 'administer site configuration'  

Create a Controller

<?php  
  
namespace Drupal\student_records\Controller;  
  
use Drupal\Core\Controller\ControllerBase;  
use Drupal\Core\Database\Database;  
  
class StudentRecordsController extends ControllerBase {  
  
  public function content() {  
    // Define table headers.  
    $header = [  
      'id' => $this->t('ID'),  
      'name' => $this->t('Name'),  
      'email' => $this->t('Email'),  
      'course' => $this->t('Course'),  
    ];  
  
    // Fetch data from custom table.  
    $rows = [];  
    $connection = Database::getConnection();  
    $query = $connection->select('student_records', 'sr')  
      ->fields('sr', ['id', 'name', 'email', 'course'])  
      ->execute();  
  
    // Populate rows array.  
    foreach ($query as $record) {  
      $rows[] = [  
        'id' => $record->id,  
        'name' => $record->name,  
        'email' => $record->email,  
        'course' => $record->course,  
      ];  
    }  
  
    // Build render array for the table.  
    $build = [  
      '#type' => 'table',  
      '#header' => $header,  
      '#rows' => $rows,  
      '#empty' => $this->t('No data found.'),  
    ];  
  
    return $build;  
  }  
}
<?php  
  
/**  
 * Implements hook_schema().  
 */  
function student_records_schema() {  
  $schema['student_records'] = [  
    'fields' => [  
      'id' => [  
        'type' => 'serial',  
        'not null' => TRUE,  
      ],  
      'name' => [  
        'type' => 'varchar',  
        'length' => 255,  
        'not null' => TRUE,  
      ],  
      'email' => [  
        'type' => 'varchar',  
        'length' => 255,  
        'not null' => TRUE,  
      ],  
      'course' => [  
        'type' => 'varchar',  
        'length' => 255,  
        'not null' => TRUE,  
      ],  
    ],  
    'primary key' => ['id'],  
  ];  
  
  return $schema;  
}
drush en student_records -y
INSERT INTO student_records (name, email, course) VALUES   
('John Doe', 'john.doe@example.com', 'Computer Science'),  
('Jane Smith', 'jane.smith@example.com', 'Mathematics'),  
('Alice Johnson', 'alice.johnson@example.com', 'Physics');

One comment

Leave a Reply

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

×