diff --git a/app/Models/CategoryModal.php b/app/Models/CategoryModal.php index e4ec51c1..38ac9387 100644 --- a/app/Models/CategoryModal.php +++ b/app/Models/CategoryModal.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; /** * 分类表 @@ -13,4 +14,13 @@ class CategoryModal extends Model protected $table = 'categories'; protected $guarded = []; + + /** + * 项目 + * @return HasMany + */ + public function projects(): HasMany + { + return $this->hasMany(ProjectModal::class, 'category_id', 'id'); + } } diff --git a/app/Models/FrameworkModal.php b/app/Models/FrameworkModal.php index 3a54a396..9fedc885 100644 --- a/app/Models/FrameworkModal.php +++ b/app/Models/FrameworkModal.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; /** * 框架表 @@ -13,4 +14,13 @@ class FrameworkModal extends Model protected $table = 'frameworks'; protected $guarded = []; + + /** + * 项目关联 + * @return BelongsToMany + */ + public function projects() + { + return $this->belongsToMany(ProjectModal::class, 'project_frameworks_relations', 'framework_id', 'project_id'); + } } diff --git a/app/Models/LabelModal.php b/app/Models/LabelModal.php index 2cba0f74..23886959 100644 --- a/app/Models/LabelModal.php +++ b/app/Models/LabelModal.php @@ -3,6 +3,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; /** * 标签表 @@ -13,4 +15,13 @@ class LabelModal extends Model protected $table = 'labels'; protected $guarded = []; + + /** + * 项目关联 + * @return BelongsToMany + */ + public function projects(): BelongsToMany + { + return $this->belongsToMany(ProjectModal::class, 'project_labels_relations', 'label_id', 'project_id'); + } } diff --git a/app/Models/OrderModal.php b/app/Models/OrderModal.php index ee351bdc..4b3c62b9 100644 --- a/app/Models/OrderModal.php +++ b/app/Models/OrderModal.php @@ -3,6 +3,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; /** * 订单表 @@ -13,4 +15,22 @@ class OrderModal extends Model protected $table = 'orders'; protected $guarded = []; + + /** + * 用户关联 + * @return HasOne + */ + public function user() + { + return $this->hasOne(UserModal::class, 'id', 'user_id'); + } + + /** + * 订单详情 + * @return HasMany + */ + public function items(): HasMany + { + return $this->hasMany(OrderItemModal::class, 'order_id', 'id'); + } } diff --git a/app/Models/ProjectFrameworksRelationModal.php b/app/Models/ProjectFrameworksRelationModal.php index 37e985df..3151d80c 100644 --- a/app/Models/ProjectFrameworksRelationModal.php +++ b/app/Models/ProjectFrameworksRelationModal.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; /** * 项目框架关联表 diff --git a/app/Models/ProjectLabelRelationModal.php b/app/Models/ProjectLabelRelationModal.php index 591f53b5..0f849a7e 100644 --- a/app/Models/ProjectLabelRelationModal.php +++ b/app/Models/ProjectLabelRelationModal.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasOne; /** * 项目标签关联表 @@ -13,4 +14,22 @@ class ProjectLabelRelationModal extends Model protected $table = 'project_label_relations'; protected $guarded = []; + + /** + * 项目 + * @return HasOne + */ + public function project(): HasOne + { + return $this->hasOne(ProjectModal::class, 'id', 'project_id'); + } + + /** + * 标签 + * @return HasOne + */ + public function label(): HasOne + { + return $this->hasOne(LabelModal::class, 'id', 'label_id'); + } } diff --git a/app/Models/ProjectModal.php b/app/Models/ProjectModal.php index 08782577..ff8296ed 100644 --- a/app/Models/ProjectModal.php +++ b/app/Models/ProjectModal.php @@ -3,6 +3,9 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; /** * 项目表 @@ -14,4 +17,75 @@ class ProjectModal extends Model protected $guarded = []; + /** + * 用户关联 + * @return HasOne + */ + public function user(): HasOne + { + return $this->hasOne(UserModal::class, 'id', 'user_id'); + } + + /** + * 作者关联 + * @return HasOne + */ + public function author(): HasOne + { + return $this->hasOne(UserModal::class, 'id', 'author_id'); + } + + /** + * 标签关联 + * @return BelongsToMany + */ + public function labels(): BelongsToMany + { + return $this->belongsToMany(LabelModal::class, 'project_labels_relations', 'project_id', 'label_id'); + } + + /** + * 截图关联 + * @return HasMany + */ + public function screenshots(): HasMany + { + return $this->hasMany(ScreenshotModal::class, 'project_id', 'id'); + } + + /** + * 框架关联 + * @return BelongsToMany + */ + public function frameworks(): BelongsToMany + { + return $this->belongsToMany(FrameworkModal::class, 'project_frameworks_relations', 'project_id', 'framework_id'); + } + + /** + * 技术栈关联 + * @return BelongsToMany + */ + public function technologyStacks(): BelongsToMany + { + return $this->belongsToMany(TechnologyStackModal::class, 'project_technology_stacks_relations', 'project_id', 'technology_stack_id'); + } + + /** + * 功能关联 + * @return HasMany + */ + public function features(): HasMany + { + return $this->hasMany(FeatureModal::class, 'project_id', 'id'); + } + + /** + * 分类关联 + * @return HasOne + */ + public function category(): HasOne + { + return $this->hasOne(CategoryModal::class, 'id', 'category_id'); + } } diff --git a/app/Models/TechnologyStackModal.php b/app/Models/TechnologyStackModal.php index 6eb9297c..8bcc4218 100644 --- a/app/Models/TechnologyStackModal.php +++ b/app/Models/TechnologyStackModal.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; /** * 技术栈表 @@ -13,4 +14,13 @@ class TechnologyStackModal extends Model protected $table = 'technology_stacks'; protected $guarded = []; + + /** + * 项目关联 + * @return BelongsToMany + */ + public function projects(): BelongsToMany + { + return $this->belongsToMany(ProjectModal::class, 'project_technology_stack_relations', 'technology_stack_id', 'project_id'); + } } diff --git a/app/Models/UserModal.php b/app/Models/UserModal.php index e459b83f..2959bb9c 100644 --- a/app/Models/UserModal.php +++ b/app/Models/UserModal.php @@ -3,6 +3,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; /** * 用户表 @@ -13,4 +15,31 @@ class UserModal extends Model protected $table = 'users'; protected $guarded = []; + + /** + * 项目关联 - 上传视角 + * @return HasMany + */ + public function projects(): HasMany + { + return $this->hasMany(ProjectModal::class, 'user_id', 'id'); + } + + /** + * 项目关联 - 作者视角 + * @return HasMany + */ + public function projectsAuthor(): HasMany + { + return $this->hasMany(ProjectModal::class, 'author_id', 'id'); + } + + /** + * 角色关联 + * @return HasOne + */ + public function roles(): HasOne + { + return $this->hasOne(RoleModal::class, 'id', 'role_id'); + } }