I have the below snippet which returns month from my created_at timestamp. I am wanting this to return as MM-YYYY but not sure of the syntax. This is in a laravel implementation. I have tried to add 'MONTH, YEAR' but no luck.
$data = [
'data' => Payment::selectSub('MONTH(created_at)', 'month')
->selectSub('SUM(payment_amount)', 'total_payments')
->groupBy('month')
->get()];
You want to use SQL's date_format
function.
selectSub('DATE_FORMAT(created_at, "%m-%Y")', 'month')
See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format for a full list of different formatting options.
Try this
$data = [
'data' => Payment::selectSub('MONTH(created_at)', 'year(created_at)')
->selectSub('SUM(payment_amount)', 'total_payments')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.