*/ protected $fillable = [ 'name', 'slug', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * Get the posts that belong to this category. * * @return BelongsToMany */ public function posts(): BelongsToMany { return $this->belongsToMany(Post::class, 'category_post', 'category_id', 'post_id') ->withTimestamps(); } /** * Scope a query to find category by slug. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $slug * @return \Illuminate\Database\Eloquent\Builder */ public function scopeBySlug($query, $slug) { return $query->where('slug', $slug); } /** * Get category by slug. * * @param string $slug * @return Category|null */ public static function findBySlug($slug) { return static::where('slug', $slug)->first(); } /** * Check if this is the event category. * * @return bool */ public function isEvent() { return $this->slug === 'event'; } /** * Check if this is the news category. * * @return bool */ public function isNews() { return $this->slug === 'news'; } /** * Check if this is the landing event category. * * @return bool */ public function isLandingEvent() { return $this->slug === 'landing-event'; } }