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

t0berius's avatar

laravel eloquent timstamps / parsing timestamps

For an existing application, storing the last login date of a user as a integer based timestamp, I would need to develop a custom laravel based module.

The data field itself named "last_visit" is storing these integer based timestamps, like: 1534501831.

On my model I already included the last_visit as a date property:

protected $dates = [
    'last_visit',
];

Now when I do this inside my controller (only debug):

    $users = User::where('last_visit', '<', Carbon::now()->subMonths(1))->->orderBy('last_visit', 'desc')->get();

    echo "<html><body>";

    foreach ($users as $user) {
        echo $user->name . " " . $user->last_visit .  "</br>";
    }

    echo "</body></html>";

I get this result:

userX 1970-01-01 00:00:00

How to make sure laravel is able to parse the correct timestamp, so I can do my query above.

0 likes
3 replies
MarkLL's avatar

If you are actually storing them as timestamps, try this...

$users = User::where('last_visit', '<', Carbon::now()->subMonths(1)->timestamp)->orderBy('last_visit', 'desc')->get();

Also there is a typo in you sample ->-> hopefully that's not in the code.

crnkovic's avatar

Okay, so depends how the last_visit is stored into your database. Is it timestamp field, is it date field, datetime field, integer, string?

Get the user where name is userX and dd($user->last_field).

I know this isn't what you want, but I recommend storing everything as datetime (or date) in your database and modifying how you need it in your backend using Carbon. Because this is how Laravel works the easiest with dates. But that's just me :-)

Cronix's avatar

Does that query actually return results? You have a double ->-> in there. I'm surprised you're not getting an error. Try dd($users) and make sure you're actually retrieving results.

Please or to participate in this conversation.