Files
2026-08-08 18:27:38 +08:00

61 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class ForumReply extends Model
{
use SoftDeletes;
protected $fillable = [
'thread_id',
'user_id',
'body',
'status',
];
protected $casts = [
'thread_id' => 'integer',
'user_id' => 'integer',
];
public const STATUS_PENDING = 'pending';
public const STATUS_APPROVED = 'approved';
public const STATUS_REJECTED = 'rejected';
/**
* 可见性作用域(同 ForumThread):
* - 审核员:全部
* - 登录用户:已审 + 自己发的
* - 游客:仅已审
*/
public function scopeVisible($query, ?User $user = null)
{
if ($user && $user->canPerm('forum.moderate')) {
return $query;
}
if ($user) {
return $query->where(function ($q) use ($user) {
$q->where('status', self::STATUS_APPROVED)
->orWhere('user_id', $user->id);
});
}
return $query->where('status', self::STATUS_APPROVED);
}
public function thread(): BelongsTo
{
return $this->belongsTo(ForumThread::class, 'thread_id');
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}