Task 5 - pivot with extra fields

This commit is contained in:
PovilasKorop
2021-11-22 08:05:15 +02:00
parent 4ec2014369
commit 4707c593a0
8 changed files with 144 additions and 0 deletions
+8
View File
@@ -59,3 +59,11 @@ Test method `test_show_roles_with_users()`.
---
## Task 5. BelongsToMany - Extra Fields in Pivot Table.
In the route `/teams`, the table should show the teams with users, each user with a few additional fields. Fix the relationship definition in `app/Models/Team.php` so that the Blade file `tasks/index.blade.php` would show the correct data.
Test method `test_teams_with_users()`.
---
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers;
use App\Models\Team;
class TeamController extends Controller
{
public function index()
{
$teams = Team::with('users')->get();
return view('teams.index', compact('teams'));
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function users()
{
// TASK: fix this by adding some extra code
return $this->belongsToMany(User::class);
}
}
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTeamsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('teams', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('teams');
}
}
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTeamUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('team_user', function (Blueprint $table) {
$table->foreignId('team_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->string('position');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('team_user');
}
}
+14
View File
@@ -0,0 +1,14 @@
<ul>
@foreach ($teams as $team)
<li>
{{ $team->name }}
<ul>
@foreach ($team->users as $user)
{{ $user->name }}:
position {{ $user->pivot->position }},
started at {{ $user->pivot->created_at }}
@endforeach
</ul>
</li>
@endforeach
</ul>
+2
View File
@@ -24,3 +24,5 @@ Route::post('tasks', [TaskController::class, 'store'])->middleware('auth');
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']);
+20
View File
@@ -5,6 +5,7 @@ namespace Tests\Feature;
use App\Models\Comment;
use App\Models\Role;
use App\Models\Task;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
@@ -64,4 +65,23 @@ class RelationshipsTest extends TestCase
$response = $this->get('/roles');
$response->assertStatus(200);
}
// TASK: pivot table with extra fields
public function test_teams_with_users()
{
$user = User::factory()->create();
$team = Team::create(['name' => 'Team 1']);
$createdAt = now()->toDateTimeString();
$position = 'Manager';
DB::table('team_user')->insert([
'team_id' => $team->id,
'user_id' => $user->id,
'position' => $position,
'created_at' => $createdAt
]);
$response = $this->get('/teams');
$response->assertSee($createdAt);
$response->assertSee($position);
}
}