$body, // 'post_id' => $this->id, // ]); $this->comments()->create(compact('body')); } public static function archives() { $archives = static::selectRaw(<<groupBy('year', 'month') ->orderByRaw('min(created_at) desc') ->get() ->toArray(); return $archives; } public function comments() { return $this->hasMany(Comment::class); } public function user() { return $this->belongsTo(User::class); } /** * Enables a "filter()" method on Post queries. */ public function scopeFilter($query, array $filters = []) { if ($month = $filters['month'] ?? NULL) { /* When parsing just a month name, Carbon returns a date built from the current date at midnight, with the month replaced by the specified month name */ $query->whereMonth('created_at', Carbon::parse($month)->month); } if ($year = (int) $filters['year'] ?? NULL) { /* When parsing just a month name, Carbon returns a date built from the current date at midnight, with the month replaced by the specified month name */ $query->whereYear('created_at', $year); } return $query; } public function tags() { return $this->belongsToMany(Tag::class); } }