diff --git a/README.md b/README.md index 4298f51..2cc4d6c 100644 --- a/README.md +++ b/README.md @@ -67,3 +67,11 @@ Test method `test_teams_with_users()`. --- +## Task 6. HasMany - Average from Field Value + +In the route `/countries`, the table should show the countries with average team size. Fix the Controller to load the relationship number, as it is expected in the Blade. + +Test method `test_countries_with_team_size()`. + +--- + diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php new file mode 100644 index 0000000..2b9be50 --- /dev/null +++ b/app/Http/Controllers/CountryController.php @@ -0,0 +1,16 @@ +hasMany(Team::class); + } +} diff --git a/app/Models/Team.php b/app/Models/Team.php index 52cbeba..c2749d2 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -9,7 +9,7 @@ class Team extends Model { use HasFactory; - protected $fillable = ['name']; + protected $fillable = ['name', 'size']; public function users() { diff --git a/database/migrations/2021_11_22_061400_create_countries_table.php b/database/migrations/2021_11_22_061400_create_countries_table.php new file mode 100644 index 0000000..41702a1 --- /dev/null +++ b/database/migrations/2021_11_22_061400_create_countries_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('countries'); + } +} 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 new file mode 100644 index 0000000..4edb998 --- /dev/null +++ b/database/migrations/2021_11_22_061411_add_country_id_to_teams_table.php @@ -0,0 +1,32 @@ +foreignId('country_id')->nullable()->constrained(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('teams', function (Blueprint $table) { + // + }); + } +} diff --git a/database/migrations/2021_11_22_061647_add_size_to_teams_table.php b/database/migrations/2021_11_22_061647_add_size_to_teams_table.php new file mode 100644 index 0000000..28011bc --- /dev/null +++ b/database/migrations/2021_11_22_061647_add_size_to_teams_table.php @@ -0,0 +1,32 @@ +integer('size')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('teams', function (Blueprint $table) { + // + }); + } +} diff --git a/resources/views/countries/index.blade.php b/resources/views/countries/index.blade.php new file mode 100644 index 0000000..176f214 --- /dev/null +++ b/resources/views/countries/index.blade.php @@ -0,0 +1,5 @@ + diff --git a/routes/web.php b/routes/web.php index 6c9f17a..46819c3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -26,3 +26,5 @@ Route::get('users/{user}', [\App\Http\Controllers\UserController::class, 'show'] Route::get('roles', [\App\Http\Controllers\RoleController::class, 'index']); Route::get('teams', [\App\Http\Controllers\TeamController::class, 'index']); + +Route::get('countries', [\App\Http\Controllers\CountryController::class, 'index']); diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index 10379ad..4bdc582 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature; use App\Models\Comment; +use App\Models\Country; use App\Models\Role; use App\Models\Task; use App\Models\Team; @@ -84,4 +85,23 @@ class RelationshipsTest extends TestCase $response->assertSee($createdAt); $response->assertSee($position); } + + // TASK: average number from the relationship + public function test_countries_with_team_size() + { + $country = Country::create(['name' => 'United Kingdom']); + Team::create([ + 'name' => 'Team 1', + 'country_id' => $country->id, + 'size' => 3 + ]); + Team::create([ + 'name' => 'Team 2', + 'country_id' => $country->id, + 'size' => 5 + ]); + + $response = $this->get('/countries'); + $response->assertSee('avg team size 4'); + } }