'Method does not exist' when using whereMonth()

Posted 3 months ago by michalis

I am following along from jeffery's tutorial on how to create an archive but trying to adapt it to my own neads

https://laracasts.com/series/laravel-from-scratch-2017/episodes/20

here is my code

public function index($id)
    {
        $campaign = Campaign::find($id);
        $files = $campaign->files->sortByDesc('created_at');

        if ($month = request('month')) {
            $files->whereMonth('created_at', Carbon::parse($month)->month);
        }

        if ($year = request('year')) {
            $files->whereYear('created_at', $year);
        }

        $archives = File::selectRaw('year(created_at) year, monthname(created_at) month, count(*) published')
            ->where('campaign_id', $id)
            ->groupBy('year', 'month')
            ->OrderByRaw('min(created_at) desc')
            ->get();

        return view('files', ['files' => $files, 'archives' => $archives]);
    }

on the front end I get

Method Illuminate\Database\Eloquent\Collection::whereMonth does not exist.

I think i understand the issue but not how to overcome it, do I need to get files with a query instead of through a relationship?

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