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

Tomi's avatar
Level 8

Is there a way to make DB::table() return Carbon Date

Hi,

just a quick question cause im unable to find an answer.

Is there a way to make DB::table() return a Carbon date like the Eloquent models. Instead of a string.

0 likes
8 replies
tomopongrac's avatar

That is possible only with eloquent models ...

1 like
Tomi's avatar
Level 8

thats what i was afraid of. :(

ahmeddabak's avatar

What you can do is fetch the data with DB::table() and then parse it with carbon

use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

$user = DB::table('users')->get()->first();
$date = Carbon::parse($user->created_at);
rawilk's avatar

@ahmeddabak - Or instead of using DB, you could just the the eloquent model to begin with, that way created_at is already casted to Carbon. Why have the model if you're not even going to use it?

1 like
tykus's avatar

There is probably an underlying reason why the OP wants to use the query builder in preference to Eloquent, and why the OP needs the dates as Carbon instances. Maybe, if we knew these reasons, we could better suggest a solution.

1 like
jlrdw's avatar

Carbon is completely independent of eloquent.

Tomi's avatar
Level 8

I feel like sometimes i reach the limit of Eloquent and im just better of going DB way. Like doing more complex joins also aliasing tables. Filtering data on joined tables so on.

As my query is behinde a Repository layer i can parse all my data fileds in the all() or paginate() function of my Repo.

Snapey's avatar

Depending on where you want to use the date, you could create a helper function?

Please or to participate in this conversation.