3

The table have a startDate column which is VARCHAR type, so need to get rows by ascending order using this column.

Tried this :

orderBy(DB::raw("DATE(startDate)"), 'ASC')

the above does not return correct order, how to pass string date in order by clouse?

Example:

'startDate' => '07-Nov-2017'

$items = DB::table("mytable")->orderBy(DB::raw("DATE(startDate)"), 'ASC')->where('userId','=',$userId)->get();

5
  • What is startDate format? Commented Dec 22, 2017 at 11:49
  • Is it Y-m-d? Show a example of startDate content. Commented Dec 22, 2017 at 11:51
  • Sorry for my mistake with MySQL! Commented Dec 22, 2017 at 12:04
  • @Laerte - Yes it is Mysql, I updated my question. Commented Dec 23, 2017 at 4:50
  • So, try the answer I posted, and check if it works... Commented Dec 23, 2017 at 17:45

2 Answers 2

8

You need to convert the date to MySQL format. Try to use DATE_FORMAT:

$items = DB::table("mytable")
           ->orderBy(DB::raw("DATE_FORMAT(startDate,'%d-%M-%Y')"), 'ASC')
           ->where('userId','=',$userId)
           ->get();
Sign up to request clarification or add additional context in comments.

4 Comments

@MKhalidJunaid - Its mysql.
@151291 In future please tag your question accordingly
ErrorException in Builder.php line 414: Illegal offset type
Why modulus here ? %d-%M-%Y
0

Gunaseelan was right, but with synthax errors, try this :

$items = DB::table("mytable")->where('userId','=',$userId)->orderByRaw("DATE_FORMAT('d-m-Y',startDate), ASC")->get();

1 Comment

I tried this but not correct - "DATE('d-m-Y',startDate), ASC" "DATE(startDate) ASC" "DATE('d-m-Y',strtotime(startDate)), ASC" "DATE(strtotime(startDate)), ASC" "DATE(strtotime(startDate)) ASC" "DATE(strtotime('startDate')) ASC"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.