I am trying to get data from database from the database using order by month and date.
My code is:
<?php $celebrity_birth = DB::table("celebrities")->select("*")->where(DB::raw("(DATE_FORMAT(date_of_birth,'%m-%d'))"),'>',date('m-d'))->orderBy("(DATE_FORMAT(date_of_birth,'%m-%d')","desc")->limit('4')->get(); ?>
This DATE_FORMAT can not work properly in Laravel. How can I do for it?
In laravel if your using raw query then you need to use DB::raw. Apply this code in the order_by
<?php DB::raw("DATE_FORMAT(date_of_birth,'%m-%d')"), 'asc' ?>
For example:
<?php $celebrity_birth = DB::table("celebrities")->select("*") ->where(DB::raw("(DATE_FORMAT(date_of_birth,'%m-%d'))"),'>',date('m-d')) ->orderBy(DB::raw("DATE_FORMAT(date_of_birth,'%m-%d')"), 'asc')->limit('4')->get(); ?>
Question and answer is powered by AnsPress.io