mirror of
https://github.com/10h30/Test-Eloquent-Relationships.git
synced 2026-06-05 15:07:42 +09:00
Task 8 - belongstomany add
This commit is contained in:
@@ -75,7 +75,7 @@ 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.
|
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.
|
||||||
|
|
||||||
@@ -83,3 +83,11 @@ 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()`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -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';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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'];
|
||||||
|
}
|
||||||
@@ -52,4 +52,9 @@ class User extends Authenticatable
|
|||||||
{
|
{
|
||||||
// TASK: add the code here for two-level relationship
|
// TASK: add the code here for two-level relationship
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function projects()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Project::class)->withPivot('start_date');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,3 +30,5 @@ Route::get('teams', [\App\Http\Controllers\TeamController::class, 'index']);
|
|||||||
Route::get('countries', [\App\Http\Controllers\CountryController::class, 'index']);
|
Route::get('countries', [\App\Http\Controllers\CountryController::class, 'index']);
|
||||||
|
|
||||||
Route::get('attachments', [\App\Http\Controllers\AttachmentController::class, 'index']);
|
Route::get('attachments', [\App\Http\Controllers\AttachmentController::class, 'index']);
|
||||||
|
|
||||||
|
Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store'])->middleware('auth');
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Tests\Feature;
|
|||||||
use App\Models\Attachment;
|
use App\Models\Attachment;
|
||||||
use App\Models\Comment;
|
use App\Models\Comment;
|
||||||
use App\Models\Country;
|
use App\Models\Country;
|
||||||
|
use App\Models\Project;
|
||||||
use App\Models\Role;
|
use App\Models\Role;
|
||||||
use App\Models\Task;
|
use App\Models\Task;
|
||||||
use App\Models\Team;
|
use App\Models\Team;
|
||||||
@@ -133,4 +134,23 @@ class RelationshipsTest extends TestCase
|
|||||||
$response->assertSee('Task');
|
$response->assertSee('Task');
|
||||||
$response->assertSee('Comment');
|
$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()
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user