From 841f74607e6d0a1bd5e5f2fcb2c4765a0fe31008 Mon Sep 17 00:00:00 2001 From: maged-web Date: Thu, 2 May 2024 04:51:57 +0300 Subject: [PATCH 1/2] answers --- app/Http/Controllers/CountryController.php | 4 +++- app/Http/Controllers/ProjectController.php | 5 ++++- app/Http/Controllers/UserController.php | 2 +- app/Models/Attachment.php | 1 + app/Models/Role.php | 2 +- app/Models/Team.php | 2 +- app/Models/User.php | 5 ++++- database/migrations/2021_11_22_050733_create_tasks_table.php | 4 ++-- .../2021_11_22_061411_add_country_id_to_teams_table.php | 1 + phpunit.xml | 4 ++-- resources/views/tasks/index.blade.php | 2 +- tests/Feature/RelationshipsTest.php | 2 +- 12 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 2b9be507..ca932703 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -3,13 +3,15 @@ namespace App\Http\Controllers; use App\Models\Country; +use Illuminate\Support\Facades\DB; 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..cd019be5 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Project; use Illuminate\Http\Request; class ProjectController extends Controller @@ -10,7 +11,9 @@ 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(); + $project=Project::findOrFail($request->project_id); + $user->projects()->attach($project,['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/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/Team.php b/app/Models/Team.php index 13969525..c0448b98 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..f93782ff 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; @@ -45,12 +46,14 @@ class User extends Authenticatable public function tasks() { // TASK: fix this by adding a parameter - return $this->hasMany(Task::class); + return $this->hasMany(Task::class,'user_id'); } public function comments() { // TASK: add the code here for two-level relationship + return $this->hasManyThrough(Comment::class, Task::class); + } public function projects() diff --git a/database/migrations/2021_11_22_050733_create_tasks_table.php b/database/migrations/2021_11_22_050733_create_tasks_table.php index c136b799..b36726d6 100644 --- a/database/migrations/2021_11_22_050733_create_tasks_table.php +++ b/database/migrations/2021_11_22_050733_create_tasks_table.php @@ -16,8 +16,8 @@ public function up() Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->unsignedBigInteger('users_id')->nullable(); - $table->foreign('users_id')->references('id')->on('users'); + $table->unsignedBigInteger('user_id')->nullable(); + $table->foreign('user_id')->references('id')->on('users'); $table->timestamps(); }); } diff --git a/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php b/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php index 4edb9986..5b9f40fc 100644 --- a/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php +++ b/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php @@ -27,6 +27,7 @@ public function down() { Schema::table('teams', function (Blueprint $table) { // + }); } } diff --git a/phpunit.xml b/phpunit.xml index a0b799dd..f105351e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -18,8 +18,8 @@ - - + + diff --git a/resources/views/tasks/index.blade.php b/resources/views/tasks/index.blade.php index 8d229417..9f0b8419 100644 --- a/resources/views/tasks/index.blade.php +++ b/resources/views/tasks/index.blade.php @@ -1,5 +1,5 @@
    @foreach ($tasks as $task) -
  • {{ $task->name }} ({{ $task->user->name }})
  • +
  • {{ $task->name }} ({{ optional($task->user)->name?:" " }})
  • @endforeach
diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index c7bc63f1..6216bbde 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -43,7 +43,7 @@ public function test_show_users_comments() { $user = User::factory()->create(); $task = Task::create([ - 'users_id' => $user->id, + 'user_id' => $user->id, 'name' => 'Some task' ]); Comment::create([ From 5a8511cbe05eb532125d55023dd0cd7ff0e18ec0 Mon Sep 17 00:00:00 2001 From: maged-web Date: Thu, 2 May 2024 05:35:47 +0300 Subject: [PATCH 2/2] V2 --- app/Http/Controllers/CountryController.php | 4 +--- app/Http/Controllers/ProjectController.php | 3 ++- app/Models/Attachment.php | 2 +- app/Models/User.php | 6 ++---- .../migrations/2021_11_22_050733_create_tasks_table.php | 4 ++-- .../2021_11_22_061411_add_country_id_to_teams_table.php | 1 - phpunit.xml | 4 ++-- resources/views/tasks/index.blade.php | 2 +- tests/Feature/RelationshipsTest.php | 2 +- 9 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index ca932703..49e68134 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -3,15 +3,13 @@ namespace App\Http\Controllers; use App\Models\Country; -use Illuminate\Support\Facades\DB; class CountryController extends Controller { public function index() { // TASK: load the relationship average of team size - - $countries = Country::withAvg('teams', 'size')->get(); + $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 cd019be5..7b083f33 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,9 +11,10 @@ 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 =auth()->user(); $project=Project::findOrFail($request->project_id); $user->projects()->attach($project,['start_date'=>$request->start_date]); + return 'Success'; } } diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php index b899a4c4..365d6c23 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -13,7 +13,7 @@ class Attachment extends Model public function attachable() { - // TASK: fill in the code to make it work return $this->morphTo(); + // TASK: fill in the code to make it work } } diff --git a/app/Models/User.php b/app/Models/User.php index f93782ff..5ad8057d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,7 +4,6 @@ use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; @@ -46,14 +45,13 @@ class User extends Authenticatable public function tasks() { // TASK: fix this by adding a parameter - return $this->hasMany(Task::class,'user_id'); + 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); - + return $this->hasManyThrough(Comment::class,Task::class,'users_id'); } public function projects() diff --git a/database/migrations/2021_11_22_050733_create_tasks_table.php b/database/migrations/2021_11_22_050733_create_tasks_table.php index b36726d6..c136b799 100644 --- a/database/migrations/2021_11_22_050733_create_tasks_table.php +++ b/database/migrations/2021_11_22_050733_create_tasks_table.php @@ -16,8 +16,8 @@ public function up() Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->unsignedBigInteger('user_id')->nullable(); - $table->foreign('user_id')->references('id')->on('users'); + $table->unsignedBigInteger('users_id')->nullable(); + $table->foreign('users_id')->references('id')->on('users'); $table->timestamps(); }); } diff --git a/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php b/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php index 5b9f40fc..4edb9986 100644 --- a/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php +++ b/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php @@ -27,7 +27,6 @@ public function down() { Schema::table('teams', function (Blueprint $table) { // - }); } } diff --git a/phpunit.xml b/phpunit.xml index f105351e..a0b799dd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -18,8 +18,8 @@ - - + + diff --git a/resources/views/tasks/index.blade.php b/resources/views/tasks/index.blade.php index 9f0b8419..f05863e9 100644 --- a/resources/views/tasks/index.blade.php +++ b/resources/views/tasks/index.blade.php @@ -1,5 +1,5 @@
    @foreach ($tasks as $task) -
  • {{ $task->name }} ({{ optional($task->user)->name?:" " }})
  • +
  • {{ $task->name }} ({{ optional($task->user)->name?: ' '}})
  • @endforeach
diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index 6216bbde..c7bc63f1 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -43,7 +43,7 @@ public function test_show_users_comments() { $user = User::factory()->create(); $task = Task::create([ - 'user_id' => $user->id, + 'users_id' => $user->id, 'name' => 'Some task' ]); Comment::create([