Taks 4 - belongsToMany pivot

This commit is contained in:
PovilasKorop
2021-11-22 07:47:31 +02:00
parent 0fbdf0c99a
commit 4ec2014369
8 changed files with 128 additions and 0 deletions
+8
View File
@@ -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()`.
---
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers;
use App\Models\Role;
class RoleController extends Controller
{
public function index()
{
$roles = Role::withCount('users')->get();
return view('roles.index', compact('roles'));
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function users()
{
// TASK: fix this by adding a parameter
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 CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users_roles', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
$table->foreignId('role_id')->constrained();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users_roles');
}
}
+5
View File
@@ -0,0 +1,5 @@
<ul>
@foreach ($roles as $role)
<li>{{ $role->name }} ({{ $role->users_count }})</li>
@endforeach
</ul>
+2
View File
@@ -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']);
+16
View File
@@ -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);
}
}