Implement Eloquent models and relationships

Added Community, Post, Comment, and Vote models with proper relationships.
Updated User model to include karma field and forum-related relationships.
This commit is contained in:
m1ngsama 2025-11-29 14:15:00 +08:00
parent 34f73d3207
commit b453c1020a
5 changed files with 220 additions and 10 deletions

56
app/Models/Comment.php Normal file
View file

@ -0,0 +1,56 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
protected $fillable = [
'post_id',
'user_id',
'parent_id',
'content',
'votes',
];
protected function casts(): array
{
return [
'votes' => '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();
}
}

50
app/Models/Community.php Normal file
View file

@ -0,0 +1,50 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Community extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'description',
'created_by',
];
protected static function boot()
{
parent::boot();
static::creating(function ($community) {
if (empty($community->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';
}
}

53
app/Models/Post.php Normal file
View file

@ -0,0 +1,53 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'community_id',
'user_id',
'title',
'content',
'url',
'type',
'votes',
];
protected function casts(): array
{
return [
'votes' => '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();
}
}

View file

@ -21,23 +21,14 @@ class User extends Authenticatable
'name',
'email',
'password',
'karma',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
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);
}
}

35
app/Models/Vote.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Vote extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'voteable_id',
'voteable_type',
'vote',
];
protected function casts(): array
{
return [
'vote' => 'integer',
];
}
public function user()
{
return $this->belongsTo(User::class);
}
public function voteable()
{
return $this->morphTo();
}
}