paacnok
6 months ago

Filter scopes don't show even months.

Posted 6 months ago by paacnok

I was followign laracasts filters lesson.

I can filter all my posts. I can display on the sidebar correctly each Month and Year and even the post count for each.All displays correctly.

But when i click on the sidebar in the even months ( February, April, June, August, October, December) the posts with these months don't show up OR they show the next following month post.

E.g.: I click February and March post shows.

I click March and March post shows.

I click April and May post shows .

I click May and May post shows.

My controller index funtion

public function index()
    {
        $recipes = Recipe::latest()
        ->filter(request()->only(['month', 'year']))
        ->paginate(6);

        return view('mainnav.home', compact('recipes')); 
    }

My model filters


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']);
        }
        

    }

public static function archives(){

     return static::selectRaw('year(created_at) year, monthname(created_at) month, count(*) published')
            ->groupBy('year', 'month')
            ->orderByRaw('min(created_at) desc')
            ->get()
            ->toArray();

    }

My html

<ul class="category-list list-icons">
      @foreach ($archives as $published)

            <li>
            <a href="/?month={{$published['month']}}&year={{$published['year']}}">
                    <i class="ion-ios-arrow-right"></i>
                    {{$published['month']}} {{$published['year']}}
                </a>
            </li>
       @endforeach
           
</ul>       
            

My dd($archives)

array:15 [▼
  0 => array:3 [▼
    "year" => 2019
    "month" => "March"
    "published" => 1
  ]
  1 => array:3 [▼
    "year" => 2019
    "month" => "February"
    "published" => 1
  ]
  2 => array:3 [▼
    "year" => 2019
    "month" => "January"
    "published" => 1
  ]
  3 => array:3 [▼
    "year" => 2018
    "month" => "December"
    "published" => 1
  ]
  4 => array:3 [▼
    "year" => 2018
    "month" => "November"
    "published" => 1
  ]
  5 => array:3 [▼
    "year" => 2018
    "month" => "October"
    "published" => 1
  ]
  6 => array:3 [▼
    "year" => 2018
    "month" => "September"
    "published" => 1
  ]
  7 => array:3 [▼
    "year" => 2018
    "month" => "August"
    "published" => 1
  ]
  8 => array:3 [▼
    "year" => 2018
    "month" => "July"
    "published" => 1
  ]
  9 => array:3 [▼
    "year" => 2018
    "month" => "June"
    "published" => 1
  ]
  10 => array:3 [▼
    "year" => 2018
    "month" => "May"
    "published" => 1
  ]
  11 => array:3 [▼
    "year" => 2018
    "month" => "April"
    "published" => 1
  ]
  12 => array:3 [▼
    "year" => 2018
    "month" => "March"
    "published" => 1
  ]
  13 => array:3 [▼
    "year" => 2018
    "month" => "February"
    "published" => 1
  ]
  14 => array:3 [▼
    "year" => 2018
    "month" => "January"
    "published" => 1
  ]
]

Please spare another pair of eyes or your insight in what I'm doign wrong.

Thank you for your time

Please sign in or create an account to participate in this conversation.