102 lines
2.4 KiB
PHP
102 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Blog;
|
|
use App\Models\User;
|
|
use App\Models\Comment as Modelself;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Comment extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = ['message', 'ip', 'user_agent', 'status', 'name', 'blog_id', 'seen', 'parent_id', 'type', 'likes', 'admin' , 'image_url'];
|
|
|
|
public function blog()
|
|
{
|
|
return $this->belongsTo(Blog::class);
|
|
}
|
|
|
|
public function admin()
|
|
{
|
|
return $this->belongsTo(User::class, 'admin', 'id');
|
|
}
|
|
|
|
protected $appends = [
|
|
'sub_comments',
|
|
];
|
|
|
|
public function getSubCommentsAttribute()
|
|
{
|
|
return $this->other()->get();
|
|
}
|
|
|
|
public function other()
|
|
{
|
|
return $this->hasMany(Comment::class, 'parent_id', 'id');
|
|
}
|
|
|
|
public function hasPendingReplies()
|
|
{
|
|
foreach ($this->other as $reply) {
|
|
if ($reply->status === 0 || $reply->hasPendingReplies()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function scopeParent(Builder $query): void
|
|
{
|
|
$query->where('type', 0);
|
|
}
|
|
|
|
public function scopeAnswer(Builder $query): void
|
|
{
|
|
$query->where('type', 1);
|
|
}
|
|
|
|
public function scopeReply(Builder $query): void
|
|
{
|
|
$query->where('type', 2);
|
|
}
|
|
|
|
public function getTypeCheckAttribute()
|
|
{
|
|
if ($this->type == 0) {
|
|
return 'کامنت';
|
|
} else if ($this->type == 1) {
|
|
return 'جواب';
|
|
} else if ($this->type == 2) {
|
|
return 'ریپلای';
|
|
}
|
|
return 'نامشخص';
|
|
}
|
|
|
|
public function getTypeStatusAttribute()
|
|
{
|
|
if ($this->status == 0) {
|
|
return 'در انتظار بررسی';
|
|
} else if ($this->status == 1) {
|
|
return 'تایید';
|
|
} else if ($this->status == 2) {
|
|
return 'رد';
|
|
}
|
|
return 'نا مشخص';
|
|
}
|
|
public function getTypeStatuscolorAttribute()
|
|
{
|
|
if ($this->status == 0) {
|
|
return 'text-slate-400';
|
|
} else if ($this->status == 1) {
|
|
return ' text-green-500';
|
|
} else if ($this->status == 2) {
|
|
return 'text-red-500';
|
|
}
|
|
return 'text-blue-400';
|
|
}
|
|
}
|