76 lines
1.8 KiB
PHP
76 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class ForumThread extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'board_id',
|
|
'user_id',
|
|
'title',
|
|
'body',
|
|
'status',
|
|
'is_pinned',
|
|
'is_locked',
|
|
'views',
|
|
'last_reply_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'board_id' => 'integer',
|
|
'user_id' => 'integer',
|
|
'is_pinned' => 'boolean',
|
|
'is_locked' => 'boolean',
|
|
'views' => 'integer',
|
|
'last_reply_at' => 'datetime',
|
|
];
|
|
|
|
public const STATUS_PENDING = 'pending';
|
|
public const STATUS_APPROVED = 'approved';
|
|
public const STATUS_REJECTED = 'rejected';
|
|
|
|
/**
|
|
* 可见性作用域:
|
|
* - 审核员(forum.moderate):看全部(含待审/驳回)
|
|
* - 登录用户:看已审 + 自己发的(无论状态)
|
|
* - 游客:仅看已审
|
|
*/
|
|
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 board(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ForumBoard::class, 'board_id');
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function replies(): HasMany
|
|
{
|
|
return $this->hasMany(ForumReply::class, 'thread_id');
|
|
}
|
|
}
|