Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/Http/Controllers/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

use App\Models\Country;

class CountryController extends Controller
{
public function index()
{
class CountryController extends Controller {
// app/Http/Controllers/CountryController.php
public function index() {
// TASK: load the relationship average of team size
$countries = Country::all();
$countries = Country::with('teams')->get();
$countries = $countries->map(function ($country) {
$country->teams_avg_size = $country->teams->avg('size');

return $country;
});

return view('countries.index', compact('countries'));
}
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Illuminate\Http\Request;

class ProjectController extends Controller
{
public function store(Request $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

auth()->user()->projects()->attach($request->project_id, ['start_date' => $request->start_date]);

return 'Success';
}
}
11 changes: 4 additions & 7 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

use App\Models\User;

class UserController extends Controller
{
public function index()
{
$users = User::all();
class UserController extends Controller {
public function index() {
$users = User::has('projects')->get();

return view('users.index', compact('users'));
}

public function show(User $user)
{
public function show(User $user) {
return view('users.show', compact('user'));
}
}
7 changes: 3 additions & 4 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Attachment extends Model
{
class Attachment extends Model {
use HasFactory;

protected $fillable = ['filename', 'attachable_id', 'attachable_type'];

public function attachable()
{
public function attachable() {
// TASK: fill in the code to make it work
return $this->morphTo();
}
}
14 changes: 9 additions & 5 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Task;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Comment extends Model
{
class Comment extends Model {
use HasFactory;

protected $fillable = ['task_id', 'name', 'comment'];

public function task()
{
public function task() {
return $this->belongsTo(Task::class);
}

public function user() {
return $this->belongsTo(User::class, 'users_id');
}
}
8 changes: 3 additions & 5 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
class Role extends Model {
use HasFactory;

protected $fillable = ['name'];

public function users()
{
public function users() {
// TASK: fix this by adding a parameter
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class, 'users_roles');
}
}
12 changes: 5 additions & 7 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Team extends Model
{
class Team extends Model {
use HasFactory;

protected $fillable = ['name', 'size', 'country_id'];

public function users()
{
public function users() {
// TASK: fix this by adding some extra code
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class, 'team_user')->withPivot('position', 'created_at');
}

}
24 changes: 12 additions & 12 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace App\Models;

use App\Models\Role;
use App\Models\Task;
use App\Models\Comment;
use App\Models\Project;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
class User extends Authenticatable {
use HasApiTokens, HasFactory, Notifiable;

/**
Expand Down Expand Up @@ -42,19 +45,16 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];

public function tasks()
{
public function tasks() {
// 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
public function comments() {
return $this->hasManyThrough(Comment::class, Task::class, 'users_id', 'task_id');
}

public function projects()
{
public function projects() {
return $this->belongsToMany(Project::class)->withPivot('start_date');
}
}
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 }} ({{ optional($task->user)->name }})</li>
@endforeach
</ul>