From 0dea98939f341f78594e380b7ef028ff7dcd34e0 Mon Sep 17 00:00:00 2001 From: Stelarde Date: Fri, 13 Sep 2024 21:45:51 +0300 Subject: [PATCH] Laravel Eloquent Relationships test --- app/Http/Controllers/CountryController.php | 2 +- app/Http/Controllers/ProjectController.php | 2 +- app/Http/Controllers/TeamController.php | 2 +- app/Http/Controllers/UserController.php | 3 +-- app/Models/Attachment.php | 4 +++- app/Models/Role.php | 5 +++-- app/Models/Task.php | 2 +- app/Models/User.php | 6 ++++-- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/CountryController.php b/app/Http/Controllers/CountryController.php index 2b9be507..49e68134 100644 --- a/app/Http/Controllers/CountryController.php +++ b/app/Http/Controllers/CountryController.php @@ -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')); } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e04fb1a6..f63679d4 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -10,7 +10,7 @@ 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 - + $request->user()->projects()->attach($request->project_id,['start_date'=>$request->start_date]); return 'Success'; } } diff --git a/app/Http/Controllers/TeamController.php b/app/Http/Controllers/TeamController.php index 04e26b60..3924b934 100644 --- a/app/Http/Controllers/TeamController.php +++ b/app/Http/Controllers/TeamController.php @@ -8,7 +8,7 @@ class TeamController extends Controller { public function index() { - $teams = Team::with('users')->get(); + $teams = Team::with('users','name','email')->get(); return view('teams.index', compact('teams')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7ae1d3d6..5c688488 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -8,8 +8,7 @@ class UserController extends Controller { public function index() { - $users = User::all(); - + $users = User::has('projects')->get(); return view('users.index', compact('users')); } diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php index 158b6470..5332039d 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; class Attachment extends Model { @@ -11,8 +12,9 @@ class Attachment extends Model protected $fillable = ['filename', 'attachable_id', 'attachable_type']; - public function attachable() + public function attachable(): MorphTo { // TASK: fill in the code to make it work + return $this->morphTo(__FUNCTION__, 'attachable_type', 'attachable_id'); } } diff --git a/app/Models/Role.php b/app/Models/Role.php index c2f3fc89..0338be82 100644 --- a/app/Models/Role.php +++ b/app/Models/Role.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Role extends Model { @@ -11,9 +12,9 @@ class Role extends Model protected $fillable = ['name']; - public function users() + public function users(): BelongsToMany { // TASK: fix this by adding a parameter - return $this->belongsToMany(User::class); + return $this->belongsToMany(User::class, 'users_roles', 'role_id', 'user_id'); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 01f6912d..619eea62 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -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(); } } diff --git a/app/Models/User.php b/app/Models/User.php index 3d7facd2..20e1cb48 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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 { @@ -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->hasMany(Comment::class,'id'); } public function projects()