make static analyz for blogPost

This commit is contained in:
2024-01-10 14:42:34 +03:00
parent 93ca684f67
commit fb3670a549
14 changed files with 120 additions and 69 deletions

View File

@@ -10,6 +10,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
/**
* App\Models\BlogPost
@@ -57,17 +59,22 @@ class BlogPost extends Model implements Viewable
{
use HasFactory, InteractsWithViews;
protected $withCount = [
'likes',
'comments',
];
// protected array $withCount = [
// 'likes',
// 'comments',
// ];
public static function getWithLikesAndCommentsCount(): Builder
{
return self::withCount(['likes', 'comments']);
}
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function author()
public function author(): HasOneThrough
{
return $this
->hasOneThrough(User::class, BlogPostAuthor::class, 'blog_post_id', 'id', 'id', 'user_id')
@@ -90,7 +97,7 @@ class BlogPost extends Model implements Viewable
return $this->hasMany(BlogPostLike::class);
}
public function isLiked(): bool
public function isLiked(): bool|int
{
$curUser = auth()->user();
if ($curUser) {