1

I want to show photos grouped by date. It works, but only with two queries and without pagination. Is there a way to do it with only one query, with laravels paginate() ? I need the last 5 days (only those that have pictures), and then always 5 days per page.

The problem is, that i want to paginate the days, not the pictures.

    $days = Pic::select(DB::raw('DATE(created_at) as datum'))
                   ->distinct()
                   ->orderBy('datum','desc')
                   ->take(5)
                   ->get();

$daysarray = array();
foreach($days as $day)
{
   $pics = Pic::where(DB::raw('DATE(created_at)'),'=',$day->datum)
                    ->orderBy('id','desc')
                    ->get();

   $daysarray[$day->datum] = $pics;
}

2 Answers 2

5

I believe you will have to live with two queries in order to paginate the dates. The following should work for that bit.

$days = Pic::select(DB::raw('DATE(created_at) as datum'))
    ->distinct()
    ->orderBy('datum','desc')
    ->paginate(5);
1
  • 1
    It gave me a wrong number of pages. (too many) I had to replace ->distinct() by groupBy('datum'). Now it works.
    – haheute
    Commented Nov 22, 2013 at 15:59
0
                    $selectboxtwo = DB::table('downloads')
                    ->distinct('date')
                    ->groupBy('date')
                    ->lists('date');
                    dd($selectboxtwo); // making sure if it works. you can see the following array is produced.

array(3) { [0]=> string(10) "2014-03-09" [1]=> string(10) "2015-06-22" [2]=> string(10) "2015-06-23" }

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.