Lets assume we are creating 2 tables in Laravel 11 Database in mysql
create table tbl_school_master with following columns (school_id, school_name, school_email, school_password, school_fysm, city, country, pincode, phone, currency, created_on)
and tbl_student_master with
(student_id
, student_name
, roll_no
, reg_no
, school_id
, student_phone
, student_email
, parent_name
, date_of_birth
, type
, gender
, religion
, caste
, password
, active
, created_on
, admission_date
,photograph
)
To create the tables tbl_school_master and tbl_student_master in your Laravel project, you can use Laravel migrations. Here’s how you can define the migrations for these tables.
Steps:
Generate Migrations: Use Artisan to generate a migration file for each table:
php artisan make:migration create_tbl_school_master_table
php artisan make:migration create_tbl_student_master_table
Edit the Migration Files: After running the above commands, Laravel will generate two migration files in the database/migrations/ directory. Open these files and define the table structures.
Migration for tbl_school_master
In the file create_tbl_school_master_table.php:
php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTblSchoolMasterTable extends Migration
{
public function up()
{
Schema::create('tbl_school_master', function (Blueprint $table) {
$table->id('school_id'); // Auto-incrementing primary key
$table->string('school_name'); // School name
$table->string('school_email')->unique(); // Unique email
$table->string('school_password'); // Password (hashed)
$table->string('school_fysm')->nullable(); // Fiscal Year Start Month (optional)
$table->string('city'); // City
$table->string('country'); // Country
$table->string('pincode', 10); // Pincode with a max length of 10
$table->string('phone', 15); // Phone number with max length of 15
$table->string('currency', 3); // Currency code (e.g., USD, INR)
$table->timestamp('created_on')->useCurrent(); // Timestamp for creation
});
}
public function down()
{
Schema::dropIfExists('tbl_school_master');
}
}
Migration for tbl_student_master
In the file create_tbl_student_master_table.php:
php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTblStudentMasterTable extends Migration
{
public function up()
{
Schema::create('tbl_student_master', function (Blueprint $table) {
$table->id('student_id'); // Auto-incrementing primary key
$table->string('student_name'); // Student name
$table->string('roll_no')->unique(); // Unique roll number
$table->string('reg_no')->unique(); // Unique registration number
$table->unsignedBigInteger('school_id'); // Foreign key to tbl_school_master
$table->string('student_phone', 15); // Phone number with max length of 15
$table->string('student_email')->unique(); // Unique email
$table->string('parent_name'); // Parent's name
$table->date('date_of_birth'); // Date of birth
$table->enum('type', ['day', 'boarding'])->default('day'); // Type of student (day or boarding)
$table->enum('gender', ['male', 'female', 'other']); // Gender of the student
$table->string('religion')->nullable(); // Religion (optional)
$table->string('caste')->nullable(); // Caste (optional)
$table->string('password'); // Password (hashed)
$table->boolean('active')->default(true); // Whether the student is active
$table->timestamp('created_on')->useCurrent(); // Timestamp for creation
$table->date('admission_date'); // Date of admission
$table->string('photograph')->nullable(); // File path to photograph
$table->foreign('school_id')->references('school_id')->on('tbl_school_master')->onDelete('cascade'); // Foreign key constraint
});
}
public function down()
{
Schema::dropIfExists('tbl_student_master');
}
}
Running the Migration
Once the migration files are set up, run the following command to create the tables in the database:
bash
php artisan migrate
Explanation of Columns:
tbl_school_master:
school_id: Primary key (auto-incrementing).
school_name: Name of the school.
school_email: Unique email for the school.
school_password: Hashed password for the school's login.
school_fysm: Fiscal Year Start Month (optional).
city, country, pincode, phone, currency: Address and contact details.
created_on: Timestamp when the school was created.
tbl_student_master:
student_id: Primary key (auto-incrementing).
student_name: Name of the student.
roll_no: Unique roll number for the student.
reg_no: Unique registration number for the student.
school_id: Foreign key that links to the tbl_school_master.
student_phone, student_email: Contact details of the student.
parent_name: Name of the student's parent or guardian.
date_of_birth: Date of birth of the student.
type: Defines whether the student is a "day" or "boarding" student.
gender, religion, caste: Personal attributes.
password: Hashed password for student login.
active: Whether the student account is active.
admission_date: The date the student was admitted.
photograph: Path to the student's photograph file.
Next Steps:
Seeders: You might want to create seeders to populate the database with initial data.
Eloquent Models: Laravel will use models for these tables (School and Student). You can generate models by running:
bash
php artisan make:model School
php artisan make:model Student