mirror of
https://github.com/10h30/Test-Eloquent-Relationships.git
synced 2026-06-05 15:07:42 +09:00
Task 3 - multi-level relationships
This commit is contained in:
@@ -43,3 +43,11 @@ Test method `test_task_with_no_user()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 3. Two-level Relationship.
|
||||
|
||||
In the route `/users/{user}`, the table should load the comments that are written on the task that belong to a user. Define the relationship from User to Comment in the User model, so that the Blade file users/show.blade.php would work.
|
||||
|
||||
Test method `test_show_users_comments()`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function show(User $user)
|
||||
{
|
||||
return view('users.show', compact('user'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Comment extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['task_id', 'name', 'comment'];
|
||||
|
||||
public function task()
|
||||
{
|
||||
return $this->belongsTo(Task::class);
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,12 @@ class User extends Authenticatable
|
||||
|
||||
public function tasks()
|
||||
{
|
||||
// TASK: fix this by adding a parameter
|
||||
return $this->hasMany(Task::class);
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
// TASK: add the code here for two-level relationship
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateCommentsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('comments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('task_id')->constrained();
|
||||
$table->string('name');
|
||||
$table->text('comment');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('comments');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<h2>{{ $user->name }}</h2>
|
||||
|
||||
<b>Comments on their tasks:</b>
|
||||
<ul>
|
||||
@foreach ($user->comments as $comment)
|
||||
<li>{{ $comment->name }} ({{ $comment->comment }})</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@@ -20,3 +20,5 @@ Route::get('/', function () {
|
||||
|
||||
Route::get('tasks', [TaskController::class, 'index']);
|
||||
Route::post('tasks', [TaskController::class, 'store'])->middleware('auth');
|
||||
|
||||
Route::get('users/{user}', [\App\Http\Controllers\UserController::class, 'show']);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
@@ -29,4 +30,22 @@ class RelationshipsTest extends TestCase
|
||||
$response = $this->get('/tasks');
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
// TASK: define the two-level relationship in the User model
|
||||
public function test_show_users_comments()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$task = Task::create([
|
||||
'users_id' => $user->id,
|
||||
'name' => 'Some task'
|
||||
]);
|
||||
Comment::create([
|
||||
'task_id' => $task->id,
|
||||
'name' => 'Some name',
|
||||
'comment' => 'Some comment'
|
||||
]);
|
||||
|
||||
$response = $this->get('/users/' . $user->id);
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user