php/app/Models/Post.php
m1ngsama b453c1020a 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.
2025-11-29 14:15:00 +08:00

53 lines
941 B
PHP

<?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();
}
}