diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 2b9be507..df5e650f 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -9,8 +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..92808802 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -2,7 +2,10 @@ namespace App\Http\Controllers; +use App\Models\Project; +use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class ProjectController extends Controller { @@ -10,6 +13,11 @@ 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 + DB::table('project_user')->insert([ + 'project_id' => $request->project_id, + 'start_date' => $request->start_date, + 'user_id' => auth()->user()->id, + ]); return 'Success'; } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7ae1d3d6..1cf3cb89 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -3,12 +3,13 @@ namespace App\Http\Controllers; use App\Models\User; +use Illuminate\Database\Eloquent\Builder; 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..d30d9a63 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; class Attachment extends Model { @@ -11,8 +12,9 @@ class Attachment extends Model protected $fillable = ['filename', 'attachable_id', 'attachable_type']; - public function attachable() + public function attachable():MorphTo { // TASK: fill in the code to make it work + return $this->morphTo(); } } 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/Team.php b/app/Models/Team.php index 13969525..a1744706 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->hasMany(User::class); } } diff --git a/app/Models/User.php b/app/Models/User.php index 3d7facd2..f792e3f4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -45,12 +45,12 @@ class User extends Authenticatable public function tasks() { // TASK: fix this by adding a parameter - return $this->hasMany(Task::class); + return $this->hasOne(Task::class, 'users_id', 'id'); } public function comments() { - // TASK: add the code here for two-level relationship + return $this->tasks->hasMany(Comment::class); } public function projects() diff --git a/resources/views/tasks/index.blade.php b/resources/views/tasks/index.blade.php index 8d229417..1c5602b7 100644 --- a/resources/views/tasks/index.blade.php +++ b/resources/views/tasks/index.blade.php @@ -1,5 +1,5 @@