123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App;
- use Carbon\Carbon;
- class Post extends Model
- {
-
- protected $fillable = ['body', 'title', 'user_id'];
-
- public function addComment(string $body)
- {
- $this->comments()->create(compact('body'));
- }
- public function comments()
- {
- return $this->hasMany(Comment::class);
- }
- public function user()
- {
- return $this->belongsTo(User::class);
- }
-
- public function scopeFilter($query, array $filters = [])
- {
- if ($month = $filters['month'] ?? NULL)
- {
-
- $query->whereMonth('created_at', Carbon::parse($month)->month);
- }
- if ($year = (int) $filters['year'] ?? NULL)
- {
-
- $query->whereYear('created_at', $year);
- }
- return $query;
- }
- }
|