Create Database Tables Migration in Laravel
This snippet shows how to create database tables using Laravel's migration feature. It defines three tables: 'users', 'categories', and 'courses', along with their respective columns and data types. This is essential for structuring the database for a web application built with Laravel.iamvu
php
10 months ago
3.4 kB
11
Indexable
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDatabaseTables extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('Name');
$table->string('Email')->unique();
$table->string('Password');
$table->string('Role');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('Name');
$table->text('Description')->nullable();
});
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->string('Title');
$table->text('Description');
$table->float('Price');
$table->string('Thumbnail');
$table->foreignId('InstructorID')->constrained('users')->onDelete('cascade');
$table->foreignId('CategoryID')->constrained('categories')->onDelete('cascade');
$table->timestamps();
});
Schema::create('enrollments', function (Blueprint $table) {
$table->id();
$table->foreignId('CourseID')->constrained('courses')->onDelete('cascade');
$table->foreignId('UserID')->constrained('users')->onDelete('cascade');
$table->dateTime('EnrollmentDate');
$table->float('Progress')->default(0);
});
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->foreignId('UserID')->constrained('users')->onDelete('cascade');
$table->dateTime('OrderDate');
$table->float('TotalAmount');
$table->string('PaymentStatus');
});
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->foreignId('OrderID')->constrained('orders')->onDelete('cascade');
$table->string('PaymentMethod');
$table->float('PaymentAmount');
$table->dateTime('PaymentDate');
$table->string('PaymentStatus');
});
Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->foreignId('CourseID')->constrained('courses')->onDelete('cascade');
$table->foreignId('UserID')->constrained('users')->onDelete('cascade');
$table->integer('Rating');
$table->text('Comment')->nullable();
$table->dateTime('ReviewDate');
});
Schema::create('lessons', function (Blueprint $table) {
$table->id();
$table->foreignId('CourseID')->constrained('courses')->onDelete('cascade');
$table->string('Title');
$table->text('Content');
$table->integer('OrderNumber');
});
}
public function down()
{
Schema::dropIfExists('lessons');
Schema::dropIfExists('reviews');
Schema::dropIfExists('payments');
Schema::dropIfExists('orders');
Schema::dropIfExists('enrollments');
Schema::dropIfExists('courses');
Schema::dropIfExists('categories');
Schema::dropIfExists('users');
}
}
Editor is loading...
Leave a Comment