diff --git a/README.md b/README.md index 90f495c..8310462 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,11 @@ Test method `test_show_users_comments()`. --- +## Task 4. BelongsToMany - Pivot Table Name. + +In the route `/roles`, the table should load the roles with the number of users belonging to them. But the relationship in `app/Models/Role.php` model is defined incorrectly, fix that relationship definition. + +Test method `test_show_roles_with_users()`. + +--- + diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php new file mode 100644 index 0000000..c8f98c6 --- /dev/null +++ b/app/Http/Controllers/RoleController.php @@ -0,0 +1,15 @@ +get(); + + return view('roles.index', compact('roles')); + } +} diff --git a/app/Models/Role.php b/app/Models/Role.php new file mode 100644 index 0000000..c2f3fc8 --- /dev/null +++ b/app/Models/Role.php @@ -0,0 +1,19 @@ +belongsToMany(User::class); + } +} diff --git a/database/migrations/2021_11_22_053957_create_roles_table.php b/database/migrations/2021_11_22_053957_create_roles_table.php new file mode 100644 index 0000000..d85b7cf --- /dev/null +++ b/database/migrations/2021_11_22_053957_create_roles_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('roles'); + } +} diff --git a/database/migrations/2021_11_22_054017_create_users_roles_table.php b/database/migrations/2021_11_22_054017_create_users_roles_table.php new file mode 100644 index 0000000..da1d63d --- /dev/null +++ b/database/migrations/2021_11_22_054017_create_users_roles_table.php @@ -0,0 +1,31 @@ +foreignId('user_id')->constrained(); + $table->foreignId('role_id')->constrained(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users_roles'); + } +} diff --git a/resources/views/roles/index.blade.php b/resources/views/roles/index.blade.php new file mode 100644 index 0000000..f2095ea --- /dev/null +++ b/resources/views/roles/index.blade.php @@ -0,0 +1,5 @@ + diff --git a/routes/web.php b/routes/web.php index 0d12836..ceade0a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -22,3 +22,5 @@ Route::get('tasks', [TaskController::class, 'index']); 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']); diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index 1d806e9..d979579 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -3,9 +3,11 @@ namespace Tests\Feature; use App\Models\Comment; +use App\Models\Role; use App\Models\Task; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\DB; use Tests\TestCase; class RelationshipsTest extends TestCase @@ -48,4 +50,18 @@ class RelationshipsTest extends TestCase $response = $this->get('/users/' . $user->id); $response->assertStatus(200); } + + // TASK: pivot table name in the list + public function test_show_roles_with_users() + { + $user = User::factory()->create(); + $role = Role::create(['name' => 'Admin']); + DB::table('users_roles')->insert([ + 'role_id' => $role->id, + 'user_id' => $user->id + ]); + + $response = $this->get('/roles'); + $response->assertStatus(200); + } }