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

skoobi's avatar
Level 13

Eloquent query on a created_at column

Hi. I'm working on a legacy database where I need to grab rows differently based on the date.

I.e. For anything before 2022, i need to grab the 'transaction_type' column where it contains 'DB' and then anything after the start of this year, I need to grab the 'transaction_type' column contains 'CR'

Is there a way of doing this?

I saw the ->when() function, but seems this isn't what i want as i cannot query the column with a given date.

I've not used accessors before, but would this be the way to do it, or is there a better way?

many thanks

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You would need a raw expression with a CASE statement

1 like
skoobi's avatar
Level 13

@tykus Brilliant, thank you. Keep forgetting I can use raw expressions with Laravel. Last time i used it was in v4

Please or to participate in this conversation.