php/app/Models/Community.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

50 lines
955 B
PHP

<?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';
}
}