diff --git a/README.md b/README.md index 46f888e..9c72af4 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,11 @@ class User extends Model use HasTasks; } +class Project extends Model +{ + use HasTasks; +} + // Create a task $user->addTask( [ @@ -36,6 +41,9 @@ $user->addTask( ] ); +// You can also attach tasks to other models +$project->addTask(['name' => 'Deploy new version']); + // Get all tasks $user->tasks; diff --git a/tests/HasTasksTest.php b/tests/HasTasksTest.php index e4f1901..2dab652 100644 --- a/tests/HasTasksTest.php +++ b/tests/HasTasksTest.php @@ -20,4 +20,16 @@ public function test_model_can_create_and_retrieve_tasks() $this->assertTrue($user->tasks->first()->is($task)); $this->assertCount(1, $user->pendingTasks()); } + + public function test_task_owner_is_polymorphic() + { + $project = Project::create(['name' => 'Test Project']); + + $task = $project->addTask(['name' => 'Project Task']); + + $this->assertInstanceOf(Task::class, $task); + $this->assertEquals($project->id, $task->owner_id); + $this->assertEquals(Project::class, $task->owner_class); + $this->assertTrue($task->owner->is($project)); + } } diff --git a/tests/Project.php b/tests/Project.php new file mode 100644 index 0000000..861bc9b --- /dev/null +++ b/tests/Project.php @@ -0,0 +1,15 @@ +timestamps(); }); + Schema::create('projects', function (Blueprint $table) { + $table->id(); + $table->string('name')->nullable(); + $table->timestamps(); + }); + // run package migrations $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); }