Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

avishkaMe's avatar

Display existing months jobs first (DESC) and then the rest sorted by date DESC

Hi there, I have a question that, I first need to show the existing month jobs first in DESC order in the data table and, then the rest of the jobs according to DESC order. Can anyone help me with this?

0 likes
12 replies
SilenceBringer's avatar

@avishkame Which column you want to sort by? Is it date column? It may contains dates in the past and in the future?

avishkaMe's avatar

@SilenceBringer Imagine your job table is having the records of the past month, if we do this way past month records appear first. but I need to show current month jobs first in the table and then the other jobs in DESC order. Any Idea?

SilenceBringer's avatar

@avishkaMe latest will sorts by dates in desc order. Current month is the latest one (it no future dates exists). So, current month will be the first one, and then - previous months

Did you try it?

SilenceBringer's avatar

@avishkaMe 20 hours ago I asked it

It may contains dates in the past and in the future?

and you answered me that column with date is created_at

created_at

how created_at can be in the future?

do you want to display jobs with future date or not?

avishkaMe's avatar

@SilenceBringer Oh I'm really sorry, I really appreciate your help, the column with date is not created_at, it's a custom column named start_date, which stores the job starting day.

I need to sort the data table according to that column. and that contains past and future days. Again I really appreciate your contribution in this case.

SilenceBringer's avatar
Level 55

@avishkame just in case - try something like this

Job::orderByRaw('if(month(start_date) = month(curdate()), 1, 0) desc')
	->orderBy('start_date', 'desc')
	->get()
1 like
SilenceBringer's avatar

@avishkaMe you're welcome. Pay attention to questions, your correct answers can save a lot of time to find the solution

1 like

Please or to participate in this conversation.