Create a select based on groupBy 'created_at'

Posted 2 years ago by RushVan

Wondering if this is even the best approach for this...

I am trying to generate a select list by grouping records by 'created_at' so that users can select records by year.

In my controller I am doing this;

        $years = Campaign::select('created_at')
        ->groupBy(function($date) {
            return Carbon::parse($date->created_at)->format('Y'); // grouping by years

This returns this collection;

Collection {#188 ▼
  #items: array:3 [▼
    2016 => Collection {#174 ▶}
    2015 => Collection {#185 ▶}
    2014 => Collection {#190 ▶}

Then in my view I am generating the select like this;

 {{ Form::select('year', $years, null, (['class'=>'form-control'])) }}

The issue - this then populates the select with the full array of values for each year. What I was looking for was just the actual year value.

How do I get to that? Thanks!

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