diff --git a/app/Models/Comment.php b/app/Models/Comment.php new file mode 100644 index 0000000..e6d61f3 --- /dev/null +++ b/app/Models/Comment.php @@ -0,0 +1,56 @@ + 'integer', + ]; + } + + public function post() + { + return $this->belongsTo(Post::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } + + public function parent() + { + return $this->belongsTo(Comment::class, 'parent_id'); + } + + public function replies() + { + return $this->hasMany(Comment::class, 'parent_id'); + } + + public function votes() + { + return $this->morphMany(Vote::class, 'voteable'); + } + + public function userVote($userId) + { + return $this->votes()->where('user_id', $userId)->first(); + } +} diff --git a/app/Models/Community.php b/app/Models/Community.php new file mode 100644 index 0000000..41bde63 --- /dev/null +++ b/app/Models/Community.php @@ -0,0 +1,50 @@ +slug)) { + $community->slug = Str::slug($community->name); + } + }); + } + + public function creator() + { + return $this->belongsTo(User::class, 'created_by'); + } + + public function subscribers() + { + return $this->belongsToMany(User::class)->withTimestamps(); + } + + public function posts() + { + return $this->hasMany(Post::class); + } + + public function getRouteKeyName() + { + return 'slug'; + } +} diff --git a/app/Models/Post.php b/app/Models/Post.php new file mode 100644 index 0000000..cfe5213 --- /dev/null +++ b/app/Models/Post.php @@ -0,0 +1,53 @@ + 'integer', + ]; + } + + public function community() + { + return $this->belongsTo(Community::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } + + public function comments() + { + return $this->hasMany(Comment::class); + } + + public function votes() + { + return $this->morphMany(Vote::class, 'voteable'); + } + + public function userVote($userId) + { + return $this->votes()->where('user_id', $userId)->first(); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 749c7b7..5140b85 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -21,23 +21,14 @@ class User extends Authenticatable 'name', 'email', 'password', + 'karma', ]; - /** - * The attributes that should be hidden for serialization. - * - * @var list - */ protected $hidden = [ 'password', 'remember_token', ]; - /** - * Get the attributes that should be cast. - * - * @return array - */ protected function casts(): array { return [ @@ -45,4 +36,29 @@ protected function casts(): array 'password' => 'hashed', ]; } + + public function communities() + { + return $this->hasMany(Community::class, 'created_by'); + } + + public function subscribedCommunities() + { + return $this->belongsToMany(Community::class)->withTimestamps(); + } + + public function posts() + { + return $this->hasMany(Post::class); + } + + public function comments() + { + return $this->hasMany(Comment::class); + } + + public function votes() + { + return $this->hasMany(Vote::class); + } } diff --git a/app/Models/Vote.php b/app/Models/Vote.php new file mode 100644 index 0000000..0e90a90 --- /dev/null +++ b/app/Models/Vote.php @@ -0,0 +1,35 @@ + 'integer', + ]; + } + + public function user() + { + return $this->belongsTo(User::class); + } + + public function voteable() + { + return $this->morphTo(); + } +}