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

akmadhwa's avatar

select Raw data with date_format

I need to select data with dateformat example :

 $query = $this->model::selectRaw('DATE_FORMAT(customer.created_at, "%Y-%m-%d %H:%i"));

But it keep on returning data mising, my table for created_at column contain data like 2020-03-13 11:07:23.

Do you all happen to know the right way to write the query do share me the result. Thank you

0 likes
8 replies
Nakov's avatar

@akmadhwa is your table called customer or customers?

Why do you need to specify it?

Just this does not work:

 $query = $this->model::selectRaw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i"));

What is your model instance here?

tykus's avatar

Eloquent models cast their timestamp fields created_at and updated_at to Carbon instances - which is assumed to be the database default Y-m-d H:i:s (in most circumstances). Because you are changing the format, Carbon cannot create an instance using the default format, hence the data missing error. If you want to modify the datetime for display purposes, do it at an appropriate level in your application, e.g. using an accessor method in the model class, or at the presentation level in your view template, or JSON transformer.

akmadhwa's avatar

oh so if i want to retrieve a data with date_format of 'Y-m-d H:i" only is it possible. My target right now is to retrieve data and ignore the second value.

Nakov's avatar

@akmadhwa just add this one to your model:

public function getCreatedAtAttribute($value)
{
    return Carbon::parse($value)->format('Y-m-d H:i');
}

And then use it:

$user = User::first();
$user->created_at; // 2020-03-27 12:20
2 likes
akmadhwa's avatar

Thank you @nakov i tried your way and i could not get any output from table, it keep on saying data missing. I tried to make query from my mysql to check if my query is right or not and the query return a data. But when i tried putting it inside my selectRaw it does not return any value? Could you help me with this.

My real intention is just to get data within the date range (Y-m-d H:i) of created_at, I cannot change or add column into the table as the data already exist.

akmadhwa's avatar
akmadhwa
OP
Best Answer
Level 3

I manage to find the solution, If i want to select the created_at at certain format just use this way

$query = $this->model::selectRaw('DATE_FORMAT(created_at,"%Y-%m-%d %H:%i:00"));

This way i am able to get value in certain range of time without select or check on the second

2 likes
Nepean's avatar

In this post, i share with you that how to use date_format() with where condition of Laravel query builder. When i was working on my project, i require to search with get only selected month and year records from created_at(timestamp) column https://www.prepaidgiftbalance.vip/

1 like
Vishal_Chuadhary's avatar

$Vehicle =$this->model::SelectRaw('DATE_FORMAT'('date',"%Y-%m-%d"))->where('user_id', $request->user_id)->where('station_id', $request->station_id)->where('date', $request->date)->get();

where should i print my model name

Please or to participate in this conversation.