From c219949e1b0653b7b1663477acfa30991c6cfbf2 Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:33:40 +0800 Subject: [PATCH 01/10] Update User.php --- app/Models/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/User.php b/app/Models/User.php index 3d7facd2..b48466b3 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -45,7 +45,7 @@ class User extends Authenticatable public function tasks() { // TASK: fix this by adding a parameter - return $this->hasMany(Task::class); + return $this->hasMany(Task::class,'users_id'); } public function comments() From 4c710b6d69a6c79a9a52ed8772bba5012125b0f7 Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:34:33 +0800 Subject: [PATCH 02/10] Update Task.php --- app/Models/Task.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Models/Task.php b/app/Models/Task.php index 01f6912d..0b4ac17a 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -13,6 +13,8 @@ class Task extends Model public function user() { - return $this->belongsTo(User::class, 'users_id'); + return $this->belongsTo(User::class, 'users_id')->withDefault([ + 'name'=>'' + ]); } } From 41c554eb5cb07e514c671f647ec219251c477991 Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:36:42 +0800 Subject: [PATCH 03/10] Update User.php --- app/Models/User.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Models/User.php b/app/Models/User.php index b48466b3..531b5a2b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -51,6 +51,7 @@ public function tasks() public function comments() { // TASK: add the code here for two-level relationship + return $this->hasManyThrough(Comment::class,Task::class,'users_id','task_id'); } public function projects() From 77fb2a7310bbe58a1dcca1b8e4fe057b48f1dbf1 Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:38:27 +0800 Subject: [PATCH 04/10] Update Role.php --- app/Models/Role.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Role.php b/app/Models/Role.php index c2f3fc89..da294f07 100644 --- a/app/Models/Role.php +++ b/app/Models/Role.php @@ -14,6 +14,6 @@ class Role extends Model public function users() { // TASK: fix this by adding a parameter - return $this->belongsToMany(User::class); + return $this->belongsToMany(User::class,'users_roles','role_id','user_id'); } } From ccbdd4f99d4c9bb477e6dadd9da365729d6972de Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:39:33 +0800 Subject: [PATCH 05/10] Update Team.php --- app/Models/Team.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Team.php b/app/Models/Team.php index 13969525..ee3ca599 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -14,7 +14,7 @@ class Team extends Model public function users() { // TASK: fix this by adding some extra code - return $this->belongsToMany(User::class); + return $this->belongsToMany(User::class)->withPivot(['position','created_at']); } } From fea3332a3490f5d99c5193234387ea6a4248cd3c Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:41:18 +0800 Subject: [PATCH 06/10] Update CountryController.php --- app/Http/Controllers/CountryController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 2b9be507..37be30c4 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -9,7 +9,7 @@ class CountryController extends Controller public function index() { // TASK: load the relationship average of team size - $countries = Country::all(); + $countries = Country::withAvg('teams','size'); return view('countries.index', compact('countries')); } From dae0aad7292279653fa803ec1293a6ba7b086f5b Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:42:46 +0800 Subject: [PATCH 07/10] Update Attachment.php --- app/Models/Attachment.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php index 158b6470..b899a4c4 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -14,5 +14,6 @@ class Attachment extends Model public function attachable() { // TASK: fill in the code to make it work + return $this->morphTo(); } } From 87e8e54948063b815c5348ac496aee7a18be0bda Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:44:34 +0800 Subject: [PATCH 08/10] Update ProjectController.php --- app/Http/Controllers/ProjectController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e04fb1a6..1180650a 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -10,6 +10,7 @@ public function store(Request $request) { // TASK: Add one sentence to save the project to the logged-in user // by $request->project_id and with $request->start_date parameter + $request->user()->projects()->attach($request->project_id,['start_date'=>$request->start_date]); return 'Success'; } From ee75ee99a77fa0394227ab6cb27c2bbfe4d1ea87 Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:45:20 +0800 Subject: [PATCH 09/10] Update UserController.php --- app/Http/Controllers/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7ae1d3d6..b97f4a1c 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -8,7 +8,7 @@ class UserController extends Controller { public function index() { - $users = User::all(); + $users = User::has('projects')->get(); return view('users.index', compact('users')); } From b4a385f83dd1596bfcd20efac8d8042db4ec97c6 Mon Sep 17 00:00:00 2001 From: Donald <154738239+DonaldFon@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:47:08 +0800 Subject: [PATCH 10/10] Update CountryController.php --- app/Http/Controllers/CountryController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 37be30c4..49e68134 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -9,7 +9,7 @@ class CountryController extends Controller public function index() { // TASK: load the relationship average of team size - $countries = Country::withAvg('teams','size'); + $countries = Country::withAvg('teams','size')->get(); return view('countries.index', compact('countries')); }