Skip to content

Commit 10e0503

Browse files
committed
create model, seeder and migration for Subject, Node and Resource
1 parent 1fa0654 commit 10e0503

10 files changed

Lines changed: 314 additions & 9 deletions

‎.env.example‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ LOG_STACK=single
2020
LOG_DEPRECATIONS_CHANNEL=null
2121
LOG_LEVEL=debug
2222

23-
DB_CONNECTION=sqlite
24-
# DB_HOST=127.0.0.1
25-
# DB_PORT=3306
26-
# DB_DATABASE=laravel
27-
# DB_USERNAME=root
28-
# DB_PASSWORD=
23+
DB_CONNECTION=mysql
24+
DB_HOST=127.0.0.1
25+
DB_PORT=3306
26+
DB_DATABASE=hsc_stack_db
27+
DB_USERNAME=root
28+
DB_PASSWORD=
2929

3030
SESSION_DRIVER=database
3131
SESSION_LIFETIME=120

‎app/Models/Node.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Node extends Model
8+
{
9+
//
10+
}

‎app/Models/Resource.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Resource extends Model
8+
{
9+
//
10+
}

‎app/Models/Subject.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Subject extends Model
8+
{
9+
//
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('subjects', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name',100);
17+
$table->string('slug',100)->unique();
18+
$table->string('icon',50);
19+
$table->integer('sort_order')->default(0);
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('subjects');
30+
}
31+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('nodes', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignId('subject_id')->constrained('subjects')->cascadeOnDelete();
17+
$table->foreignId('parent_id')->nullable()->constrained('nodes')->cascadeOnDelete();
18+
$table->string('name', 200);
19+
$table->string('slug', 100);
20+
$table->unique(['parent_id', 'slug']);
21+
22+
$table->integer('sort_order')->default(0);
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*/
30+
public function down(): void
31+
{
32+
Schema::dropIfExists('nodes');
33+
}
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('resources', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignId('node_id')->constrained('nodes')->cascadeOnDelete();
17+
$table->enum('resource_type', ['note', 'question', 'pdf', 'image', 'video']);
18+
$table->string('title',100);
19+
$table->text('content')->nullable();
20+
$table->string('file_url',500)->nullable();
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*/
28+
public function down(): void
29+
{
30+
Schema::dropIfExists('resources');
31+
}
32+
};

‎database/seeders/DatabaseSeeder.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function run(): void
1717
{
1818
// User::factory(10)->create();
1919

20-
User::factory()->create([
21-
'name' => 'Test User',
22-
'email' => 'test@example.com',
20+
$this->call([
21+
SubjectSeeder::class,
22+
NodeSeeder::class,
2323
]);
2424
}
2525
}

‎database/seeders/NodeSeeder.php‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Str;
8+
9+
class NodeSeeder extends Seeder
10+
{
11+
public function run(): void
12+
{
13+
$subjects = DB::table('subjects')->pluck('id', 'name');
14+
15+
$data = [
16+
'Higher Math' => [
17+
'1st Paper' => [
18+
'Matrix & Determinants',
19+
'Vectors',
20+
'Straight Lines',
21+
'Circle',
22+
'Permutations & Combinations',
23+
'Trigonometric Ratios & Identities',
24+
'Trigonometric Equations & Associated Angles',
25+
'Functions & Graphs',
26+
'Differentiation',
27+
'Integration',
28+
],
29+
'2nd Paper' => [
30+
'Real Numbers & Inequalities',
31+
'Polynomials',
32+
'Complex Numbers',
33+
'Polynomial Equations',
34+
'Binomial Theorem',
35+
'Conics',
36+
'Inverse Trigonometric Functions & Equations',
37+
'Statics',
38+
'Dynamics / Motion of Particles in a Plane',
39+
'Probability & Statistics (Measures of Dispersion)',
40+
],
41+
],
42+
43+
'Physics' => [
44+
'1st Paper' => [
45+
'Physical World & Measurement',
46+
'Vectors',
47+
'Motion in a Straight Line',
48+
'Newtonian Mechanics',
49+
'Work, Energy & Power',
50+
'Gravitation',
51+
'Properties of Matter',
52+
'Waves',
53+
'Simple Harmonic Motion',
54+
'Thermodynamics',
55+
],
56+
'2nd Paper' => [
57+
'Electrostatics',
58+
'Current Electricity',
59+
'Magnetic Effects of Current',
60+
'Electromagnetic Induction',
61+
'AC Circuits',
62+
'Modern Physics',
63+
'Semiconductor & Electronics',
64+
'Communication System',
65+
],
66+
],
67+
68+
'Chemistry' => [
69+
'1st Paper' => [
70+
'Qualitative Chemistry',
71+
'Periodic Table & Periodicity',
72+
'Chemical Bonding',
73+
'Chemical Changes / Reactions',
74+
'Chemistry in Our Life / Environmental Chemistry',
75+
],
76+
'2nd Paper' => [
77+
'Solid State',
78+
'Solutions',
79+
'Electrochemistry',
80+
'Chemical Kinetics',
81+
'Organic Chemistry Basics',
82+
'Hydrocarbons',
83+
'Alcohols, Phenols & Ethers',
84+
'Aldehydes, Ketones & Carboxylic Acids',
85+
'Amines',
86+
'Biomolecules & Applied Chemistry',
87+
],
88+
],
89+
90+
'Biology' => [
91+
'1st Paper' => [
92+
'Cell Biology',
93+
'Cell Division',
94+
'Microorganisms',
95+
'Algae & Fungi',
96+
'Bryophytes & Pteridophytes',
97+
'Gymnosperms & Angiosperms',
98+
'Plant Physiology',
99+
],
100+
'2nd Paper' => [
101+
'Human Physiology',
102+
'Blood & Circulation',
103+
'Excretion & Homeostasis',
104+
'Nervous System',
105+
'Endocrine System',
106+
'Reproduction',
107+
'Genetics & Evolution',
108+
'Ecology & Environment',
109+
],
110+
],
111+
];
112+
113+
$sort = 0;
114+
115+
foreach ($data as $subjectName => $papers) {
116+
$subjectId = $subjects[$subjectName] ?? null;
117+
if (!$subjectId) continue;
118+
119+
foreach ($papers as $paperName => $chapters) {
120+
121+
$paperId = DB::table('nodes')->insertGetId([
122+
'subject_id' => $subjectId,
123+
'parent_id' => null,
124+
'name' => $paperName,
125+
'slug' => Str::slug($paperName),
126+
'sort_order' => $sort++,
127+
'created_at' => now(),
128+
'updated_at' => now(),
129+
]);
130+
131+
foreach ($chapters as $index => $chapter) {
132+
DB::table('nodes')->insert([
133+
'subject_id' => $subjectId,
134+
'parent_id' => $paperId,
135+
'name' => $chapter,
136+
'slug' => Str::slug($chapter),
137+
'sort_order' => $index,
138+
'created_at' => now(),
139+
'updated_at' => now(),
140+
]);
141+
}
142+
}
143+
}
144+
}
145+
}

‎database/seeders/SubjectSeeder.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Str;
8+
9+
class SubjectSeeder extends Seeder
10+
{
11+
public function run(): void
12+
{
13+
$subjects = [
14+
['name' => 'Bangla', 'icon' => 'BookOpen'],
15+
['name' => 'English', 'icon' => 'PenTool'],
16+
['name' => 'Higher Math', 'icon' => 'Sigma'],
17+
['name' => 'Physics', 'icon' => 'Atom'],
18+
['name' => 'Chemistry', 'icon' => 'FlaskConical'],
19+
['name' => 'Biology', 'icon' => 'Dna'],
20+
];
21+
22+
foreach ($subjects as $index => $subject) {
23+
DB::table('subjects')->insert([
24+
'name' => $subject['name'],
25+
'slug' => Str::slug($subject['name']),
26+
'icon' => $subject['icon'],
27+
'sort_order' => $index,
28+
'created_at' => now(),
29+
'updated_at' => now(),
30+
]);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)