diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 2b9be507..9a7b1dd6 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -1,6 +1,7 @@ function ($query) { + $query->selectRaw('country_id, AVG(size) as teams_avg_size') + ->groupBy('country_id'); + }])->get(); + Log::info($countries); return view('countries.index', compact('countries')); } } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e04fb1a6..08ac73c6 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/TaskController.php b/app/Http/Controllers/TaskController.php index 05073a4d..30a0fda0 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -5,6 +5,7 @@ use App\Models\Task; use Illuminate\Http\Request; + class TaskController extends Controller { public function index() @@ -16,6 +17,7 @@ public function index() public function store(Request $request) { + // TASK: find out why this sentence fails, and fix it in Eloquent Model auth()->user()->tasks()->create([ 'name' => $request->name diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7ae1d3d6..3819959c 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -8,13 +8,16 @@ class UserController extends Controller { public function index() { - $users = User::all(); + //$users = User::all(); + + $users = User::has('projects')->get(); return view('users.index', compact('users')); } public function show(User $user) { + $user = User::with('comments')->findOrFail($user->id); return view('users.show', compact('user')); } } 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/Role.php b/app/Models/Role.php index c2f3fc89..01c7d71e 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..88714428 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->hasMany(Comment::class); } } diff --git a/app/Models/Team.php b/app/Models/Team.php index 13969525..94f4ead5 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -14,7 +14,11 @@ 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', 'updated_at'); + } + public function country() + { + return $this->belongsTo(Country::class); } } diff --git a/app/Models/User.php b/app/Models/User.php index 3d7facd2..b2818783 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -45,12 +45,13 @@ 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() { // TASK: add the code here for two-level relationship + return $this->hasManyThrough(Comment::class, Task::class,'users_id'); } public function projects() diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 57b73b54..f84d2fda 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -13,6 +13,6 @@ class DatabaseSeeder extends Seeder */ public function run() { - // \App\Models\User::factory(10)->create(); + \App\Models\User::factory(10)->create(); } } diff --git a/resources/views/countries/index.blade.php b/resources/views/countries/index.blade.php index 176f214f..4d4734a6 100644 --- a/resources/views/countries/index.blade.php +++ b/resources/views/countries/index.blade.php @@ -1,5 +1,5 @@