Skip to content

Commit f6afcff

Browse files
committed
Refactor Task model to use morphs for creator and assignee relationships
1 parent f05c308 commit f6afcff

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

database/migrations/2023_05_18_113529_add_tasks_table.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ public function up(): void
1616
$table->string('name');
1717
$table->text('description')->nullable();
1818
$table->enum('status', ['pending', 'completed', 'running'])->default('pending');
19-
$table->unsignedBigInteger('owner_id')->nullable();
20-
$table->string('owner_class')->nullable();
21-
$table->unsignedBigInteger('assignee_id')->nullable();
22-
$table->string('assignee_class')->nullable();
19+
$table->morphs('creator');
20+
$table->morphs('assignee');
2321
$table->string('job')->nullable();
2422
$table->text('job_data')->nullable();
2523
$table->timestamp('due_date')->nullable();

src/Models/Task.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Task extends Model
1515
const STATUS_COMPLETED = 'completed';
1616
const STATUS_RUNNING = 'running';
1717

18-
protected $fillable = [
18+
protected array $fillable = [
1919
'name',
2020
'description',
2121
'status',
@@ -31,20 +31,17 @@ class Task extends Model
3131
'completed_by',
3232
];
3333

34-
/**
35-
* @return MorphTo
36-
*/
3734
public function owner(): MorphTo
3835
{
39-
return $this->morphTo('owner', 'owner_class', 'owner_id');
36+
return $this->morphTo();
4037
}
4138

4239
/**
4340
* @return MorphTo
4441
*/
4542
public function assignee(): MorphTo
4643
{
47-
return $this->morphTo('assignee', 'assignee_class', 'assignee_id');
44+
return $this->morphTo();
4845
}
4946

5047
/**

0 commit comments

Comments
 (0)