same at 22.05.2023

This commit is contained in:
Anatoliy Poloz
2023-05-22 09:52:08 +00:00
parent a9601ab15d
commit 1e546fa48e
216 changed files with 246857 additions and 6014 deletions

68
app/Models/BlogPost.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/* use Illuminate\Database\Eloquent\Builder; */
use Illuminate\Database\Eloquent\Relations\HasMany;
/* use Illuminate\Database\Eloquent\Casts\Attribute; */
use CyrildeWit\EloquentViewable\InteractsWithViews;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
class BlogPost extends Model implements Viewable
{
use HasFactory, InteractsWithViews;
protected $withCount = [
'likes',
'comments'
];
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function author()
{
/* return $this->belongsToMany(User::class, 'blog_post_author', 'blog_post_id', 'user_id'); */
return $this->hasOneThrough(User::class, BlogPostAuthor::class, 'blog_post_id', 'id', 'id', 'user_id');
}
public function comments()
{
return $this->hasMany(BlogPostComment::class);
}
public function likes(): HasMany
{
return $this->hasMany(BlogPostLike::class);
}
public function isLiked(): bool
{
$curUser = auth()->user();
if ($curUser) {
return $curUser->blog_post_likes()->forBlogPost($this)->count();
}
return false;
}
public function removeLike(): bool
{
$curUser = auth()->user();
if ($curUser) {
return $curUser->blog_post_likes()->forBlogPost($this)->delete();
}
return false;
}
public function scopeContainsTag($query, $tagString): Builder
{
$tagId = Str::of($tagString)->split('/-/')->first();
return Tag::find($tagId)->blogPosts();
}
}