Home > other >  Order by created at with time 00:00:00 until 11:59:00 and date
Order by created at with time 00:00:00 until 11:59:00 and date

Time:08-26

I have problem if order by updated_at desc the lists will keep repeating in some pages but if order by VoucherID works fine not repeating.

Example if order by updated_at with time desc: updated by column date and time

**page 4** = id=12 id=13 id=14 id=15 id=16 id=17 id=18
**page 3** = id=2 id=3 id=4 id=5 id=6 id=7 id=8 id=9 id=1 id=11
**page 2** = id=25 id=26 id=27 id=28 id=29 id=30 id=31 id=32 id=33 id=10
**page 1** = id=37 id=36 id=35 id=34 id=2 id=3 id=4 id=5 id=6 id=8

Example if order by id desc: (works fine) VoucherID column

**page 4** = id=7 id=6 id=5 id=4 id=3 id=2 id=1
**page 3** = id=17 id=16 id=15 id=14 id=13 id=12 id=11 id=10 id=9 id=8
**page 2** = id=27 id=26 id=25 id=24 id=23 id=22 id=21 id=20 id=19 id=18
**page 1** = id=37 id=36 id=35 id=34 id=33 id=32 id=31 id=30 id=29 id=28

My question is how to order by column updated_at with time from 00:00:00 until 11:59:00 and date? (works fine)

VoucherController

public function index(ManageVoucherRequest $request)
{
    return view('backend.voucher.index')
        ->withvouchers($this->voucherRepository->getActivePaginated(10, 'updated_at', 'desc'));
}

VoucherRepository

public function getActivePaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
{
    return $this->model
        ->orderBy($orderBy, $sort)
        ->paginate($paged);
}

CodePudding user response:

In the data that you have shown in the image, all rows have the same value for created_at. How is the engine supposed to know, how to sort identical field values. ?

You need a second field on your order by. For example

$orderBy = 'created_at,VoucherId'

CodePudding user response:

Try:

Model::orderByRaw('HOUR(updated_at), MINUTE(updated_at), SECOND(updated_at)')->get();
  • Related