mirror of
https://github.com/10h30/Test-Eloquent-Relationships.git
synced 2026-07-11 10:46:13 +09:00
Task 7 - polymorphic
This commit is contained in:
@@ -75,3 +75,11 @@ Test method `test_countries_with_team_size()`.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Task 6. 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()`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Attachment;
|
||||||
|
|
||||||
|
class AttachmentController extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$attachments = Attachment::all();
|
||||||
|
|
||||||
|
return view('attachments.index', compact('attachments'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Attachment extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = ['filename', 'attachable_id', 'attachable_type'];
|
||||||
|
|
||||||
|
public function attachable()
|
||||||
|
{
|
||||||
|
// TASK: fill in the code to make it work
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateAttachmentsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('attachments', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('filename');
|
||||||
|
$table->morphs('attachable');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('attachments');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<ul>
|
||||||
|
@foreach ($attachments as $attachment)
|
||||||
|
<li>{{ $attachment->filename }} {{ get_class($attachment->attachable) }}</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
@@ -28,3 +28,5 @@ Route::get('roles', [\App\Http\Controllers\RoleController::class, 'index']);
|
|||||||
Route::get('teams', [\App\Http\Controllers\TeamController::class, 'index']);
|
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']);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\Attachment;
|
||||||
use App\Models\Comment;
|
use App\Models\Comment;
|
||||||
use App\Models\Country;
|
use App\Models\Country;
|
||||||
use App\Models\Role;
|
use App\Models\Role;
|
||||||
@@ -104,4 +105,32 @@ class RelationshipsTest extends TestCase
|
|||||||
$response = $this->get('/countries');
|
$response = $this->get('/countries');
|
||||||
$response->assertSee('avg team size 4');
|
$response->assertSee('avg team size 4');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TASK: polymorphic relations
|
||||||
|
public function test_attachments_polymorphic()
|
||||||
|
{
|
||||||
|
$task = Task::create(['name' => 'Some task']);
|
||||||
|
$comment = Comment::create([
|
||||||
|
'task_id' => $task->id,
|
||||||
|
'name' => 'Some name',
|
||||||
|
'comment' => 'Some comment'
|
||||||
|
]);
|
||||||
|
Attachment::create([
|
||||||
|
'filename' => 'something.jpg',
|
||||||
|
'attachable_id' => $task->id,
|
||||||
|
'attachable_type' => Task::class
|
||||||
|
]);
|
||||||
|
Attachment::create([
|
||||||
|
'filename' => 'something.pdf',
|
||||||
|
'attachable_id' => $comment->id,
|
||||||
|
'attachable_type' => Comment::class
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->get('/attachments');
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertSee('something.jpg');
|
||||||
|
$response->assertSee('something.pdf');
|
||||||
|
$response->assertSee('Task');
|
||||||
|
$response->assertSee('Comment');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user