Files
opensem/app/Models/Core/Comment.php
2024-02-22 21:28:33 +01:00

67 lines
1.3 KiB
PHP

<?php
namespace App\Models\Core;
use App\Traits\Model\HasComments;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Comment extends Model
{
use HasComments;
protected $guarded = [];
protected $casts = [
'is_approved' => 'boolean',
];
public function scopeApproved($query)
{
return $query->where('is_approved', true);
}
public function commentable(): MorphTo
{
return $this->morphTo();
}
public function commentator(): BelongsTo
{
return $this->belongsTo($this->getAuthModelName(), 'user_id');
}
public function approve()
{
$this->update([
'is_approved' => true,
]);
return $this;
}
public function disapprove()
{
$this->update([
'is_approved' => false,
]);
return $this;
}
protected function getAuthModelName()
{
if (config('comments.user_model')) {
return config('comments.user_model');
}
if (! is_null(config('auth.providers.users.model'))) {
return config('auth.providers.users.model');
}
throw new Exception('Could not determine the commentator model name.');
}
}