I was going through the laravel from scratch episode for archives.
the problem i am facing is that some times for some archive dates, no data is fetch even if the record exist in db.
the query fired is as below as per the dd(DB::getQueryLog());
array:1 [▼
0 => array:3 [▼
"query" => "select count(*) as aggregate from `posts` where (`status` = ?) and month(`created_at`) = ? and year(`created_at`) = ?"
"bindings" => array:3 [▼
0 => 1
1 => "12"
2 => 2019
]
"time" => 0.84
]
]
fetching post code :
$posts = Post::latest()->with('author')
->where(['status' => 1])
->filter(['month' => $request->month, 'year' => $request->year])
->paginate(env('RECORD_LIMIT'));
filter code from scope in Post model:
public function scopeFilter($query, $filter)
{
if ($month = $filter['month']) {
$query->whereMonth('created_at', Carbon::parse($month)->month);
}
if ($year = $filter['year']) {
$query->whereYear('created_at', Carbon::parse($year)->year);
}
}
where am i going wrong ?
below is the query fired for fetching three records:
array:3 [▼
0 => array:3 [▼
"query" => "select count(*) as aggregate from `posts` where (`status` = ?) and month(`created_at`) = ? and year(`created_at`) = ?"
"bindings" => array:3 [▼
0 => 1
1 => "04"
2 => 2019
]
"time" => 0.68
]
1 => array:3 [▼
"query" => "select * from `posts` where (`status` = ?) and month(`created_at`) = ? and year(`created_at`) = ? order by `created_at` desc limit 20 offset 0"
"bindings" => array:3 [▼
0 => 1
1 => "04"
2 => 2019
]
"time" => 0.75
]
2 => array:3 [▼
"query" => "select * from `users` where `users`.`id` in (13, 29, 64)"
"bindings" => []
"time" => 0.66
]
]