exec("
CREATE TABLE IF NOT EXISTS `students` (
`id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NOT NULL UNIQUE,
`age` INT UNSIGNED NOT NULL DEFAULT 18,
`gender` ENUM('male', 'female') NOT NULL DEFAULT 'male',
`class_name` VARCHAR(50) NULL DEFAULT 'CNTT-K18A',
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
$count = (int) $pdo->query("SELECT COUNT(*) FROM `students`")->fetchColumn();
if ($count === 0 || isset($_POST['reseed'])) {
$seedSql = "
INSERT IGNORE INTO `students` (`id`, `name`, `email`, `age`, `gender`, `class_name`, `created_at`, `updated_at`) VALUES
(1, 'Nguyễn Văn An', 'an.nguyen@example.com', 20, 'male', 'CNTT-K18A', NOW(), NOW()),
(2, 'Trần Thị Bích Ngọc', 'ngoc.tran@example.com', 19, 'female', 'CNTT-K18A', NOW(), NOW()),
(3, 'Lê Hoàng Nam', 'nam.le@example.com', 17, 'male', 'KTPM-K19B', NOW(), NOW()),
(4, 'Phạm Minh Châu', 'chau.pham@example.com', 21, 'female', 'HTTT-K17A', NOW(), NOW()),
(5, 'Hoàng Đức Trọng', 'trong.hoang@example.com', 22, 'male', 'CNTT-K18B', NOW(), NOW()),
(6, 'Vũ Thị Mai Hương', 'huong.vu@example.com', 17, 'female', 'KTPM-K19A', NOW(), NOW()),
(7, 'Đặng Quốc Khánh', 'khanh.dang@example.com', 20, 'male', 'ANM-K18A', NOW(), NOW()),
(8, 'Bùi Thanh Hà', 'ha.bui@example.com', 19, 'female', 'CNTT-K18A', NOW(), NOW()),
(9, 'Đỗ Gia Bảo', 'bao.do@example.com', 16, 'male', 'KTPM-K19B', NOW(), NOW()),
(10, 'Ngô Phương Linh', 'linh.ngo@example.com', 21, 'female', 'HTTT-K17A', NOW(), NOW());
";
$pdo->exec($seedSql);
}
$total = (int) $pdo->query("SELECT COUNT(*) FROM `students`")->fetchColumn();
$totalPages = max(1, (int) ceil($total / $perPage));
if ($page > $totalPages) {
$page = $totalPages;
}
$offset = ($page - 1) * $perPage;
$stmt = $pdo->prepare("SELECT * FROM `students` ORDER BY `id` ASC LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$students = $stmt->fetchAll();
} catch (Throwable $e) {
$dbError = $e->getMessage();
}
$modelCode = is_file(__DIR__ . '/../app/Models/Student.php') ? file_get_contents(__DIR__ . '/../app/Models/Student.php') : '';
$migrationCode = is_file(__DIR__ . '/../database/migrations/2026_01_01_000001_create_students_table.php') ? file_get_contents(__DIR__ . '/../database/migrations/2026_01_01_000001_create_students_table.php') : '';
$factoryCode = is_file(__DIR__ . '/../database/factories/StudentFactory.php') ? file_get_contents(__DIR__ . '/../database/factories/StudentFactory.php') : '';
$seederCode = is_file(__DIR__ . '/../database/seeders/StudentSeeder.php') ? file_get_contents(__DIR__ . '/../database/seeders/StudentSeeder.php') : '';
?>
Bài 04 - Model, Migration, Factory, Seeder & Eloquent Paginate(5) | Lab 06
BÀI TẬP 04
Model Student, Migration, Factory, Seeder & Eloquent paginate(5)
Tạo bảng students (name, email, age, gender), gieo 10 bản ghi mẫu và hiển thị từ MySQL kết hợp phân trang Student::paginate(5).
Lỗi kết nối CSDL: = htmlspecialchars($dbError) ?>
-
-
| #ID |
Họ và tên |
Email |
Tuổi |
Giới tính |
Created At |
| #= (int) $st['id'] ?> |
= htmlspecialchars($st['name']) ?> |
= htmlspecialchars($st['email']) ?> |
= (int) $st['age'] ?> |
= $st['gender'] === 'male' ? 'Nam (male)' : 'Nữ (female)' ?>
|
= htmlspecialchars((string) ($st['created_at'] ?? '')) ?> |
Showing = $total > 0 ? (($page - 1) * $perPage + 1) : 0 ?> to = min($page * $perPage, $total) ?> of = $total ?> results
1. Model app/Models/Student.php
= htmlspecialchars($modelCode) ?>
2. Migration 2026_01_01_000001_create_students_table.php
= htmlspecialchars($migrationCode) ?>
3. Factory database/factories/StudentFactory.php
= htmlspecialchars($factoryCode) ?>
4. Seeder database/seeders/StudentSeeder.php
= htmlspecialchars($seederCode) ?>