Skip to content

Commit

Permalink
Test all pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Porrapat committed Sep 29, 2023
1 parent f9345a8 commit 9c30172
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CountryController extends Controller
public function index()
{
// TASK: load the relationship average of team size
$countries = Country::all();
$countries = Country::withAvg('teams', 'size')->get();;

return view('countries.index', compact('countries'));
}
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

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

Auth::user()->projects()->attach($request->project_id, ['start_date' => $request->start_date]);
return 'Success';
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class UserController extends Controller
{
public function index()
{
$users = User::all();
$users = User::has('projects', '>=', 1)->get();

return view('users.index', compact('users'));
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ class Attachment extends Model
public function attachable()
{
// TASK: fill in the code to make it work
return $this->morphTo('attachable', 'attachable_type', 'attachable_id');
}
}
2 changes: 1 addition & 1 deletion app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Role extends Model
public function users()
{
// TASK: fix this by adding a parameter
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class, 'users_roles');
}
}
7 changes: 7 additions & 0 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class Task extends Model
{
Expand All @@ -15,4 +16,10 @@ public function user()
{
return $this->belongsTo(User::class, 'users_id');
}

public function comments(): HasMany
{
// TASK: fix this by adding a parameter
return $this->hasMany(Comment::class);
}
}
2 changes: 1 addition & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Team extends Model
public function users()
{
// TASK: fix this by adding some extra code
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class, 'team_user:position,created_at');
}

}
6 changes: 4 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\HasMany;

class User extends Authenticatable
{
Expand Down Expand Up @@ -42,15 +43,16 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];

public function tasks()
public function tasks(): HasMany
{
// TASK: fix this by adding a parameter
return $this->hasMany(Task::class);
return $this->hasMany(Task::class, 'users_id');
}

public function comments()
{
// TASK: add the code here for two-level relationship
return $this->hasManyThrough(Comment::class, Task::class, 'users_id');
}

public function projects()
Expand Down
2 changes: 1 addition & 1 deletion resources/views/tasks/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ul>
@foreach ($tasks as $task)
<li>{{ $task->name }} ({{ $task->user->name }})</li>
<li>{{ $task->name }} ({{ $task->user->name ?? '' }})</li>
@endforeach
</ul>

0 comments on commit 9c30172

Please sign in to comment.