From d7b740e46a20b62fc7b27747decbacd1c18a252a Mon Sep 17 00:00:00 2001 From: KunYuChang Date: Sun, 1 Dec 2024 22:35:38 +0800 Subject: [PATCH] Complete Test-Eloquent-Relationships --- README.md | 1 + app/Http/Controllers/CountryController.php | 2 +- app/Http/Controllers/ProjectController.php | 2 ++ app/Http/Controllers/UserController.php | 2 +- app/Models/Attachment.php | 1 + app/Models/Comment.php | 5 +++++ app/Models/Role.php | 2 +- app/Models/Task.php | 7 ++++++- app/Models/Team.php | 2 +- app/Models/User.php | 7 +++++-- 10 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 42741476..60471bf6 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Test method `test_attachments_polymorphic()`. --- + ## Task 8. Add BelongsToMany Row In the POST route `/projects`, the project should be saved for a logged-in user, with start_date field from $request. Write that sentence in the Controller. diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 2b9be507..e0cf38c3 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')->get(); return view('countries.index', compact('countries')); } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e04fb1a6..0652f395 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -10,6 +10,8 @@ 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 + $user = auth()->user(); + $user->projects()->attach($request->project_id, ['start_date' => $request->start_date]); return 'Success'; } 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')); } 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(); } } diff --git a/app/Models/Comment.php b/app/Models/Comment.php index aa54d5b2..db153c3f 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -15,4 +15,9 @@ public function task() { return $this->belongsTo(Task::class); } + + public function comments() + { + return $this->morphMany(Attachment::class, 'attachable'); + } } diff --git a/app/Models/Role.php b/app/Models/Role.php index c2f3fc89..24099baa 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'); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 01f6912d..14ab1c29 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -13,6 +13,11 @@ class Task extends Model public function user() { - return $this->belongsTo(User::class, 'users_id'); + return $this->belongsTo(User::class, 'users_id')->withDefault(); + } + + public function comments() + { + return $this->morphMany(Attachment::class, 'attachable'); } } diff --git a/app/Models/Team.php b/app/Models/Team.php index 13969525..d2f5abc4 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'); } } diff --git a/app/Models/User.php b/app/Models/User.php index 3d7facd2..f4c7d536 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,6 +7,8 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; class User extends Authenticatable { @@ -42,15 +44,16 @@ class User extends Authenticatable 'email_verified_at' => 'datetime', ]; - public function tasks() + public function tasks():HasMany { // TASK: fix this by adding a parameter - return $this->hasMany(Task::class); + return $this->hasMany(Task::class, 'users_id'); } 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()