From 7901346eb9781c8b48fccedc72ab9b5c145efa1a Mon Sep 17 00:00:00 2001 From: aslan Date: Wed, 29 May 2024 16:20:22 +0300 Subject: [PATCH] add relationships for models --- app/Models/Note.php | 6 ++++++ app/Models/Project.php | 14 +++++++++++++- app/Models/Task.php | 20 ++++++++++---------- app/Models/TaskStatus.php | 11 +++++++++++ app/Models/User.php | 12 ++++++++++++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/app/Models/Note.php b/app/Models/Note.php index 702666a..6ab4143 100644 --- a/app/Models/Note.php +++ b/app/Models/Note.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class Note extends Model { @@ -18,4 +19,9 @@ class Note extends Model 'department_id', 'created_by_id', ]; + + public function task(): BelongsTo + { + return $this->belongsTo(Task::class); + } } diff --git a/app/Models/Project.php b/app/Models/Project.php index be26f93..7b8b62a 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; class Project extends Model @@ -24,7 +25,18 @@ class Project extends Model return $this->belongsTo(Department::class); } - public function users() + public function users(): BelongsToMany { + return $this->belongsToMany(User::class); + } + + public function task_statuses(): hasMany + { + return $this->hasMany(TaskStatus::class); + } + + public function tasks(): hasMany + { + return $this->hasMany(Task::class); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 732224d..8b7acb2 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -27,11 +27,6 @@ class Task extends Model return $this->belongsTo(User::class, 'created_by_id'); } - public function users(): BelongsToMany - { - return $this->belongsToMany(User::class); - } - public function status(): BelongsTo { return $this->belongsTo(TaskStatus::class); @@ -42,11 +37,6 @@ class Task extends Model return $this->belongsToMany(Label::class); } - public function department(): BelongsTo - { - return $this->belongsTo(Department::class); - } - public function notes(): HasMany { return $this->hasMany('App\Models\Note', 'task_id'); @@ -56,4 +46,14 @@ class Task extends Model { return $this->hasMany('App\Models\Task', 'parent_id'); } + + public function project(): BelongsTo + { + return $this->belongsTo(Project::class); + } + + public function files(): HasMany + { + return $this->hasMany('App\Models\File', 'task_id'); + } } diff --git a/app/Models/TaskStatus.php b/app/Models/TaskStatus.php index 76e8fb2..595ea92 100644 --- a/app/Models/TaskStatus.php +++ b/app/Models/TaskStatus.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class TaskStatus extends Model { @@ -15,4 +16,14 @@ class TaskStatus extends Model 'project_id', 'created_by_id', ]; + + public function project(): BelongsTo + { + return $this->belongsTo(Project::class); + } + + public function task(): BelongsTo + { + return $this->belongsTo(Task::class); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 90155c7..35135f0 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,8 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -45,4 +47,14 @@ class User extends Authenticatable 'password' => 'hashed', ]; } + + public function department(): BelongsTo + { + return $this->belongsTo(Department::class); + } + + public function projects(): BelongsToMany + { + return $this->belongsToMany(Project::class); + } }