i am facing a critical issues. I am trying to retrieve the data from database for archive but data doesn’t show perfectly. I wrote the following script but that is not working. please help me the fix this issues.
Following this script i am showing the month and year like (August2023)
@foreach ($archive as $data)
<p><a href="<?php echo url('/');?>/detailsarchive?m={{$data['month']}}&. y={{$data['year']}}">{{$data['month'].''.$data['year'] }}</a></p>
@endforeach
Following script is route.
Route::get(‘detailsarchive/{m}/{y}/’, [FrontendController::class,
‘detailsarchive’])->name(‘detailsarchive’);
Following this controller doesn’t show any kind of result.
public function detailsarchive($m,$y)
{
$posts = Journalform::latest();
$posts->whereMonth('created_at', $m);
if($y = request('year')){
$posts->whereYear('created_at', $y);
}
$posts = $posts->get();
return view('Frontend.Date.allviewpost', compact('posts'));
}
2
Answers
In your controller method
detailsarchive($m,$y)
you are compactingposts
and in your view, you are trying to for-each loop on$archive
. That is inconsistent. Try changing the$archive
to$posts
in your view:Side Note:
Don’t assume that
Laravel
will also containarchive
like inWordPress
It seems like you’re showing a Laravel route declaration for a GET request that points to a method detailsarchive within the FrontendController class. This route has two parameters {m} and {y} which are placeholders for the month and year, respectively. The name() method is used to give the route a named reference that can be used to generate URLs in your application.
Here’s a breakdown of what this code does:
Route::get(‘detailsarchive/{m}/{y}/’, [FrontendController::class, ‘detailsarchive’]): This defines a GET route that listens for URLs in the format detailsarchive/{m}/{y}/, where {m} and {y} are placeholders for the month and year. When a request matches this URL pattern, the method detailsarchive within the FrontendController class will be executed.
->name(‘detailsarchive’): This assigns a name to the route. Naming routes is useful when you want to generate URLs for this route in your views or code using the route() helper function.
So, in your views or code, you can generate URLs for this route using the route() helper like this:
Replace $month and $year with the actual month and year values you want to use in the URL.
Remember to ensure that the FrontendController class has a method named detailsarchive that handles the logic for this route.