mirror of
https://github.com/10h30/Test-Eloquent-Relationships.git
synced 2026-07-11 10:46:13 +09:00
Task 5 - pivot with extra fields
This commit is contained in:
@@ -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()`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
@@ -24,3 +24,5 @@ Route::post('tasks', [TaskController::class, 'store'])->middleware('auth');
|
|||||||
Route::get('users/{user}', [\App\Http\Controllers\UserController::class, 'show']);
|
Route::get('users/{user}', [\App\Http\Controllers\UserController::class, 'show']);
|
||||||
|
|
||||||
Route::get('roles', [\App\Http\Controllers\RoleController::class, 'index']);
|
Route::get('roles', [\App\Http\Controllers\RoleController::class, 'index']);
|
||||||
|
|
||||||
|
Route::get('teams', [\App\Http\Controllers\TeamController::class, 'index']);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Tests\Feature;
|
|||||||
use App\Models\Comment;
|
use App\Models\Comment;
|
||||||
use App\Models\Role;
|
use App\Models\Role;
|
||||||
use App\Models\Task;
|
use App\Models\Task;
|
||||||
|
use App\Models\Team;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
@@ -64,4 +65,23 @@ class RelationshipsTest extends TestCase
|
|||||||
$response = $this->get('/roles');
|
$response = $this->get('/roles');
|
||||||
$response->assertStatus(200);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user