list group by Laravel 4.2

Par uknowcreation, il y a 10 ans


Bonjour à tous,

Petit soucis actuellement pour regrouper la list des mes années avec tous mes mois voici un petit exemple plus parlant.

2013 (21)

Acuellement dans mon controller j'utilise le query builder de laravel pour avoir mes infos

$archives= DB::table('posts')around ->select(DB::raw('YEAR(created_at) year, MONTH(created_at) month, MONTHNAME(created_at) month_name, COUNT(*) post_count'))around ->groupBy('year','month')around ->orderBy('year', 'desc')around ->get();

si je debug $archives il me retourn mon tableau d'objet
comme ceci

array (size=4) 0 => object(stdClass)[362] public 'year' => string '2016' (length=4) public 'month' => string '1' (length=1) public 'month_name' => string 'January' (length=7) public 'post_count' => string '1' (length=1) 1 => object(stdClass)[359] public 'year' => string '2016' (length=4) public 'month' => string '2' (length=1) public 'month_name' => string 'February' (length=8) public 'post_count' => string '1' (length=1) 2 => object(stdClass)[372] public 'year' => string '2015' (length=4) public 'month' => string '11' (length=2) public 'month_name' => string 'November' (length=8) public 'post_count' => string '1' (length=1) 3 => object(stdClass)[374] public 'year' => string '2015' (length=4) public 'month' => string '12' (length=2) public 'month_name' => string 'December' (length=8) public 'post_count' => string '9' (length=1)

je voudrais savoir comment serait il possible de récupérer juste mes années avec mes mois à l'intérieur pour correspondre à la liste plus haut.

Merci à tous

1 réponse

khaleader, il y a 10 ans

vous pouvez utiliser la fonction EXTRACT()
->select(DB::raw('EXTRACT(month from created_at) = ?', [value]))