skip to Main Content

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


  1. In your controller method detailsarchive($m,$y) you are compacting posts 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:

    @foreach ($posts as $data)
        <p><a href="<?php echo url('/');?>/detailsarchive?m={{$data['month']}}&. y={{$data['year']}}">{{$data['month'].''.$data['year'] }}</a></p>
    @endforeach
    

    Side Note:
    Don’t assume that Laravel will also contain archive like in WordPress

    Login or Signup to reply.
  2. 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:

    $url = route('detailsarchive', ['m' => $month, 'y' => $year]);
    

    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.

    @foreach ($archive as $data)
       <p>
         <a href="{{route('detailsarchive',['m'=>$data['month'],'y'=>$data['year']])}}"> 
                {{$data['month'].$data['year'] }}
         </a>
       </p>
    @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search