Hello!
My route:
Route::get('/posts/archives/{year}/{month}', 'PostsController@index')
->where(['year' => '[0-9]{4}', 'month' => '[A-Za-z]+'])
->name('posts.archives');
My Controller:
public function index()
{
//dd(request());
$posts = Post::latest()
->filter(request(['year','month']))
->get();
return view('posts.index', compact('posts'));
}
My View:
<a href="{{ route('posts.archives', ['year' => $archive['year'], 'month' => $archive['month']]) }}">
My Model:
public function scopeFilter($query, $filters)
{
if (isset($filters['month'])) {
$query->whereMonth('created_at', Carbon::parse($filters['month'])->month);
}
if (isset($filters['$year'])) {
$query->whereYear('created_at', $filters['$year']);
}
}
Is there a way to elegantly grab "2019" and "April" when the user visits https//dev.local/posts/archives/2019/March ?
This returns "null":
request(['year','month'])
I'd like to avoid the old fashioned way where I'd parse the $_SERVER['REQUEST_URI'] and explode() it for the fragments I'm looking for... I'm sure Laravel has something for me, right?
Thanks!