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 @@ +