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 @@ +