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

general's avatar

Passing query results to blade - edit date format

Hi everybody,

In my User table in DB I have column online where I store datetime. I pass results to blade:

$users = User::get(); return view('admin/user/index', compact('users'));

How can I format that online value before passing it to blade? I also need to add there condition to format date only when it's not NULL.

0 likes
5 replies
jekinney's avatar

Get attribute in your model will set the get value every time you query the table.

helmerdavila's avatar

In your User class add

class User extends Model
{
    protected $dates = ['your_date'];
}

Now, you can simply in blade view (or controller if you want).

$user->your_date->format('d/m/Y');
// if null == empty value, dont worry
2 likes
Nik0n's avatar

helmerdavila, could you tell me, why i have error if my date is NULL? If i have some value, it works, but if not, i have this error: "Call to a member function format() on a non-object"

Thats why i need to use this:

<td>{{ $user->birthday ? $user->birthday->format('d/m/Y') : '' }}</td>

but i think its not right

martinbean's avatar

@Nik0n This thread was nearly ** a decade** old.

Please create a new thread if you have an issue instead of replying to ancient ones where participants may not be around any more.

Please or to participate in this conversation.