Skip to content

Commit

Permalink
Added assignment associated users
Browse files Browse the repository at this point in the history
Signed-off-by: WillTheDeveloper <willthedeveloper13@gmail.com>
  • Loading branch information
WillTheDeveloper committed Sep 25, 2022
1 parent fd3be61 commit c761215
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/Http/Resources/AssignmentUserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class AssignmentUserResource extends JsonResource
{
// WORK IN PROGRESS - ONLY WANT TO SHOW USER'S NAME INSIDE PIVOT TABLE

public function toArray($request)
{
return [
'title' => $this->title,
/*'students' => $this->User->toArray()*/
];
}
}
2 changes: 1 addition & 1 deletion app/Models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Assignment extends Model

public function User()
{
return $this->belongsToMany(User::class)->withPivot('has_seen');
return $this->belongsToMany(User::class)->withPivot('has_seen', 'user_id');
}

public function Group()
Expand Down
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@

Route::get('/assignment/{id}', function ($id) {
return new \App\Http\Resources\AssignmentResource(\App\Models\Assignment::findOrFail($id));
});
Route::get('/assignment/{id}/students', function ($id) {
return new \App\Http\Resources\AssignmentUserResource(\App\Models\Assignment::findOrFail($id));
});

0 comments on commit c761215

Please sign in to comment.