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

masandikdev's avatar

Eloquent Subquery

Hi, i have model tables like this:

id|name|current|max

1 | campaign 1 | 1 | 10

2 | campaign 2 | 5 | 15

3 | campaign 3 | 10 | 10

4 | campaign 4 | 10 | 20

5 | campaign 5 | 20 | 20

i like to select all campaign that the current is not more than max

Campaign::where('current', '<', 'max')->get();

how to do that with eloquent, i need some light:

0 likes
2 replies
Cataract0523's avatar

I dont think subquerying is necessary here. You could simply make raw sql:

DB::select('select * from campaign where campaing.current < campaign.max');*/

or

Campaign::->whereRaw('table1.field1 < table1.field2')

1 like
masandikdev's avatar

@cac do the whereRaw the only solutions?

thanks anyway it should work, i dont see this ex on laravel docs, i'll double check it then. :)

Please or to participate in this conversation.