Skip to content

Commit 1f52e0a

Browse files
committed
models, migration, seeders
1 parent 84d655d commit 1f52e0a

File tree

7 files changed

+192
-6
lines changed

7 files changed

+192
-6
lines changed

app/Models/Project.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
9+
class Project extends Model
10+
{
11+
use HasFactory;
12+
13+
protected $table = 'projects';
14+
public $timestamps = false;
15+
protected $fillable = [
16+
'id', // primary key, auto-increment, integer
17+
'name', // string
18+
];
19+
20+
public function tasks(): HasMany
21+
{
22+
return $this->hasMany(Task::class);
23+
}
24+
}

app/Models/Task.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
9+
10+
class Task extends Model
11+
{
12+
use HasFactory, SoftDeletes;
13+
14+
protected $table = 'tasks';
15+
protected $fillable = [
16+
'id', // primary key, auto-increment, integer
17+
'project_id', // foreign key, integer
18+
19+
'priority', // integer
20+
'title', // string
21+
'description', // text
22+
];
23+
protected $appends = [
24+
'created',
25+
];
26+
27+
public function project(): BelongsTo
28+
{
29+
return $this->belongsTo(Project::class);
30+
}
31+
32+
public function getCreatedAttribute()
33+
{
34+
return $this->created_at->diffForHumans();
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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('projects', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::dropIfExists('projects');
26+
}
27+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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('tasks', function (Blueprint $table) {
15+
$table->id();
16+
17+
// $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
18+
$table->foreignId('project_id')->nullable()->constrained();
19+
20+
$table->integer('priority');
21+
$table->string('title');
22+
$table->text('description')->nullable();
23+
24+
$table->timestamps();
25+
$table->softDeletes();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*/
32+
public function down(): void
33+
{
34+
Schema::table('tasks', function (Blueprint $table) {
35+
if (Schema::hasColumn('tasks', 'project_id')) {
36+
$table->dropForeign(['project_id']);
37+
}
38+
});
39+
40+
Schema::dropIfExists('tasks');
41+
}
42+
};

database/seeders/DatabaseSeeder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ class DatabaseSeeder extends Seeder
1212
*/
1313
public function run(): void
1414
{
15-
// \App\Models\User::factory(10)->create();
16-
17-
// \App\Models\User::factory()->create([
18-
// 'name' => 'Test User',
19-
// 'email' => 'test@example.com',
20-
// ]);
15+
$this->call([
16+
ProjectsSeeder::class,
17+
TasksSeeder::class,
18+
]);
2119
}
2220
}

database/seeders/ProjectsSeeder.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Project;
6+
use Illuminate\Database\Seeder;
7+
8+
class ProjectsSeeder extends Seeder
9+
{
10+
/**
11+
* Run the database seeds.
12+
*/
13+
public function run(): void
14+
{
15+
for ($i = 1; $i <= 3; $i++) {
16+
Project::create([
17+
'name' => "Project $i",
18+
]);
19+
}
20+
}
21+
}

database/seeders/TasksSeeder.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Project;
6+
use App\Models\Task;
7+
use Illuminate\Database\Seeder;
8+
9+
class TasksSeeder extends Seeder
10+
{
11+
public function run(): void
12+
{
13+
$project_ids = Project::all()->pluck('id')->toArray();
14+
15+
$now = now();
16+
$tasks = [];
17+
$project_priorities = [];
18+
foreach ($project_ids as $project_id) {
19+
$project_priorities[$project_id] = 0;
20+
}
21+
22+
for ($i = 1; $i <= 10; $i++) {
23+
$project_id = $project_ids[array_rand($project_ids)];
24+
$project_priorities[$project_id]++;
25+
26+
$tasks[] = [
27+
'project_id' => $project_id,
28+
'priority' => $project_priorities[$project_id],
29+
'title' => "Task " . $project_priorities[$project_id],
30+
'description' => "Description for Task " . $project_priorities[$project_id],
31+
'created_at' => $now,
32+
'updated_at' => $now,
33+
];
34+
}
35+
36+
Task::insert($tasks);
37+
}
38+
}

0 commit comments

Comments
 (0)