mirror of
https://github.com/10h30/Test-Eloquent-Relationships.git
synced 2026-07-11 10:46:13 +09:00
Task 6 - average of the field
This commit is contained in:
@@ -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()`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Country;
|
||||
|
||||
class CountryController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// TASK: load the relationship average of team size
|
||||
$countries = Country::all();
|
||||
|
||||
return view('countries.index', compact('countries'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Country extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
public function teams()
|
||||
{
|
||||
return $this->hasMany(Team::class);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -9,7 +9,7 @@ class Team extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
protected $fillable = ['name', 'size'];
|
||||
|
||||
public function users()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateCountriesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('countries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('countries');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddCountryIdToTeamsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('teams', function (Blueprint $table) {
|
||||
$table->foreignId('country_id')->nullable()->constrained();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('teams', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddSizeToTeamsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('teams', function (Blueprint $table) {
|
||||
$table->integer('size')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('teams', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<ul>
|
||||
@foreach ($countries as $country)
|
||||
<li>{{ $country->name }} (avg team size {{ $country->teams_avg_size }})</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@@ -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']);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user