belongsTo(User::class); } public function review() { return $this->hasOne(Review::class); } public function needReview() { return PostSecurity::whereRoleId($this->user->role->id)->first(); } public function category() { return $this->belongsTo(Category::class); } public function tag() { return $this->belongsToMany(Tag::class, "posts_tags"); } public function comment() { return $this->hasMany(Comment::class); } public function like() { return $this->hasMany(Like::class); } public function getConfirmedBy() { return User::find($this->confirmed_by)->getFullName(); } public function ago() { return Carbon::parse($this->created_at)->diffForHumans(); } public function createSlug($isEdit = false) { $newSlug = Str::slug($this->title); $slugs = Post::pluck("slug"); if($isEdit) { return $newSlug; } if($slugs->contains($newSlug)) { $id = $this->latest("id")->first()->id + 1; $newSlug = $newSlug . "-" . $id; } return $newSlug; } public function isLiked($ipAddress) { $likes = $this->like->all(); foreach($likes as $like) { if($this->id == $like->post_id && $ipAddress == $like->ip_address) { return true; } } return false; } }