63 lines
1.5 KiB
PHP
63 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class ForumPost extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'forum_posts';
|
|
|
|
protected $fillable = [
|
|
'id','thread_id','user_id','content','is_edited','edited_at'
|
|
];
|
|
|
|
public $incrementing = true;
|
|
|
|
protected $casts = [
|
|
'is_edited' => 'boolean',
|
|
'edited_at' => 'datetime',
|
|
];
|
|
|
|
public function thread(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ForumThread::class, 'thread_id');
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function attachments(): HasMany
|
|
{
|
|
return $this->hasMany(ForumAttachment::class, 'post_id');
|
|
}
|
|
|
|
public function scopeInThread(Builder $query, int $threadId): Builder
|
|
{
|
|
return $query->where('thread_id', $threadId);
|
|
}
|
|
|
|
public function scopeVisible(Builder $query): Builder
|
|
{
|
|
return $query;
|
|
}
|
|
|
|
public function scopePinned(Builder $query): Builder
|
|
{
|
|
return $query->whereHas('thread', fn (Builder $threadQuery) => $threadQuery->where('is_pinned', true));
|
|
}
|
|
|
|
public function scopeRecent(Builder $query): Builder
|
|
{
|
|
return $query->orderByDesc('created_at')->orderByDesc('id');
|
|
}
|
|
}
|