Support the ongoing development of Laravel.io →
Database Eloquent Architecture

is there a way to do this with Eloquent without having to run Raw queries etc?

(select count(`win`) from ((select (`win`) from entries LIMIT startOffset, limitOffset) as t1) WHERE `win`=1);

(model for 'entries' is Entry)

specifically I want to pick a range of results then do the count query. you can't just chain it as it will do the count query and then limit it as far as i can tell.

ie as far as I can see you can't do this:

Entry::offset($startOffset)->limit($limit)->whereNotNull('prize_id')->count();

say startOffset=10 and limit = 10 that would give me 10 records in all of the records between 10 and the end of the table

what I am trying to do is count the records in that range (10-20) that match the where clause.

thanks J

Last updated 2 years ago.
0

I solved this with

 Entry::skip($startOffset)->take($limit)->get()->filter(
   function($item) { 
    return $item->prize_id != null; 
 }
)->count();

what I needed to do was filter the collection returned from the limited set of results

it would be handy if whereNotNull etc existed on Collections!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jmp909 jmp909 Joined 29 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.