Skip to content

Commit

Permalink
eloquent relationship practice
Browse files Browse the repository at this point in the history
  • Loading branch information
KiyaTilahun committed Feb 5, 2024
1 parent f9345a8 commit 2d659be
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 12 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
9 changes: 4 additions & 5 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Project;
use Illuminate\Http\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';
}
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class UserController extends Controller
{
public function index()
{
$users = User::all();
$users = User::wherehas('projects')->get();

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

public function show(User $user)
{
$user = User::with('tasks.comments')->findOrFail($user->id);
return view('users.show', compact('user'));
}
}
2 changes: 2 additions & 0 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Attachment extends Model

public function attachable()
{

return $this->morphTo();
// TASK: fill in the code to make it work
}
}
2 changes: 1 addition & 1 deletion app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Comment extends Model

public function task()
{
return $this->belongsTo(Task::class);
return $this->belongsTo(Task::class,'task');
}
}
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');
}
}
2 changes: 1 addition & 1 deletion app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Task extends Model

public function user()
{
return $this->belongsTo(User::class, 'users_id');
return $this->belongsTo(User::class, 'users_id')->withDefault();
}
}
3 changes: 2 additions & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ 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)->withPivot('position')->withTimestamps();
}

}
3 changes: 2 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ class User extends Authenticatable
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
return $this->hasManyThrough(Comment::class,Task::class,'users_id','task_id');
}

public function projects()
Expand Down

0 comments on commit 2d659be

Please sign in to comment.