diff --git a/README.md b/README.md index 6d054d6..90f495c 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,11 @@ Test method `test_task_with_no_user()`. --- +## Task 3. Two-level Relationship. + +In the route `/users/{user}`, the table should load the comments that are written on the task that belong to a user. Define the relationship from User to Comment in the User model, so that the Blade file users/show.blade.php would work. + +Test method `test_show_users_comments()`. + +--- + diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..f7cd286 --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,13 @@ +belongsTo(Task::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 75188a0..446e0f9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -44,6 +44,12 @@ class User extends Authenticatable public function tasks() { + // TASK: fix this by adding a parameter return $this->hasMany(Task::class); } + + public function comments() + { + // TASK: add the code here for two-level relationship + } } diff --git a/database/migrations/2021_11_22_052704_create_comments_table.php b/database/migrations/2021_11_22_052704_create_comments_table.php new file mode 100644 index 0000000..fc4e2f3 --- /dev/null +++ b/database/migrations/2021_11_22_052704_create_comments_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('task_id')->constrained(); + $table->string('name'); + $table->text('comment'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('comments'); + } +} diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php new file mode 100644 index 0000000..357b239 --- /dev/null +++ b/resources/views/users/show.blade.php @@ -0,0 +1,8 @@ +

{{ $user->name }}

+ +Comments on their tasks: + diff --git a/routes/web.php b/routes/web.php index c22a86d..0d12836 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,3 +20,5 @@ Route::get('/', function () { Route::get('tasks', [TaskController::class, 'index']); Route::post('tasks', [TaskController::class, 'store'])->middleware('auth'); + +Route::get('users/{user}', [\App\Http\Controllers\UserController::class, 'show']); diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index b2088da..1d806e9 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\Models\Comment; use App\Models\Task; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -29,4 +30,22 @@ class RelationshipsTest extends TestCase $response = $this->get('/tasks'); $response->assertStatus(200); } + + // TASK: define the two-level relationship in the User model + public function test_show_users_comments() + { + $user = User::factory()->create(); + $task = Task::create([ + 'users_id' => $user->id, + 'name' => 'Some task' + ]); + Comment::create([ + 'task_id' => $task->id, + 'name' => 'Some name', + 'comment' => 'Some comment' + ]); + + $response = $this->get('/users/' . $user->id); + $response->assertStatus(200); + } }