lundi 30 octobre 2017

Laravel Eloquent distinct timestamp's year

On some indexes I have selectboxes that filter results, 1 of them filters by year. But this selectbox only needs the years present in the related table. After doing some research on the net I came up with this:

public function getAvailableYears(string $column)
{
    $years = $this->model->selectRaw("substr({$column},1,4) as year")
                         ->distinct('year')
                         ->pluck('year', 'year')
                         ->toArray();

    return ['' => 'All'] + $years;
}

This perfectly produces an array ready to be inserted into Form::select()

I was wondering if there is a better way to do this. It seems to work fine since the timestamps always start with the year, but I am not sure about performance or other issues I might encounter in the future. Any expert insight would be awesome.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire