Return Year Count Even When Count is 0

Posted 5 months ago by mprythero

I'm trying to work on a chart with a few datasets, however for the purposes of asking this question, I will only include one.

At the moment if I have the following in my controller:

$maintenancesForklifts = DB::table("equipment_attachments")
                        ->join('equipment as equip', '', '=', 'equipment_attachments.unitID')
                        ->select(DB::raw('year(date) as year'), DB::raw("COUNT(*) as count"))

I will get this returned:


As you can see there are a few years missing if I went from say 2000 to the present day. Do you know how I could return the results so that they would come back with the years that have a zero count?


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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.