Skip to content

Commit

Permalink
Task 8 - belongstomany add
Browse files Browse the repository at this point in the history
  • Loading branch information
PovilasKorop committed Nov 22, 2021
1 parent fc0bf0d commit a114584
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,19 @@ Test method `test_countries_with_team_size()`.

---

## Task 6. Polymorphic Attachments
## Task 7. Polymorphic Attachments

In the route `/attachments`, the table should show the filenames and the class names of Task and Comment models. Fix the `app/Models/Attachment.php` relationship to make it work.

Test method `test_attachments_polymorphic()`.

---

## Task 8. Add BelongsToMany Row

In the POST route `/projects`, the project should be saved for a logged-in user, with start_date field from $request. Write that sentence in the Controller.

Test method `test_belongstomany_add()`.

---

16 changes: 16 additions & 0 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProjectController extends Controller
{
public function store(Request $request)
{
// TASK: Add one sentence to save the project to the logged-in user
// by $request->project_id and with $request->start_date parameter

return 'Success';
}
}
13 changes: 13 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
use HasFactory;

protected $fillable = ['name'];
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public function comments()
{
// TASK: add the code here for two-level relationship
}

public function projects()
{
return $this->belongsToMany(Project::class)->withPivot('start_date');
}
}
32 changes: 32 additions & 0 deletions database/migrations/2021_11_22_093919_create_projects_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('projects');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProjectUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('project_user', function (Blueprint $table) {
$table->foreignId('project_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->date('start_date');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('project_user');
}
}
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
Route::get('countries', [\App\Http\Controllers\CountryController::class, 'index']);

Route::get('attachments', [\App\Http\Controllers\AttachmentController::class, 'index']);

Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store'])->middleware('auth');
20 changes: 20 additions & 0 deletions tests/Feature/RelationshipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Attachment;
use App\Models\Comment;
use App\Models\Country;
use App\Models\Project;
use App\Models\Role;
use App\Models\Task;
use App\Models\Team;
Expand Down Expand Up @@ -133,4 +134,23 @@ public function test_attachments_polymorphic()
$response->assertSee('Task');
$response->assertSee('Comment');
}

// TASK: add a record to belongstomany relationship
public function test_belongstomany_add()
{
$user = User::factory()->create();
$project = Project::create(['name' => 'Some project']);

$response = $this->actingAs($user)->post('/projects', [
'project_id' => $project->id,
'start_date' => now()->toDateString()
]);
$response->assertStatus(200);

$this->assertDatabaseHas('project_user', [
'project_id' => $project->id,
'user_id' => $user->id,
'start_date' => now()->toDateString()
]);
}
}

0 comments on commit a114584

Please sign in to comment.