eloquence icon indicating copy to clipboard operation
eloquence copied to clipboard

can Countable support relation or condition ?

Open gabrieltong opened this issue 9 years ago • 5 comments
trafficstars

Countable is easy to use , but It doesn't work in my case .

post has two state : active or inactive
user can has three relation to post : all post , active posts and inactive posts, 
and I want to cache all three count

thanks

gabrieltong avatar Apr 02 '16 03:04 gabrieltong

I checked the code, it's not based on eloquent relation, but db sql, why do we use relation ? so there's no condition problem, I can put the condition in relation .

gabrieltong avatar Apr 02 '16 03:04 gabrieltong

The reason is because the SQL statement helps to avoid race condition issues where 2 requests may be executed at the same time, resulting in an incorrect count on the related table. If you're fetching an object, and then making a change and doing the update that way, you will get incorrect counts over time.

That said, I think we could also do an update() call against the relation, that could work. I think this deserves some more thought, thanks for the heads up!

kirkbushell avatar Apr 02 '16 09:04 kirkbushell

I also need to apply a condition for counting related rows. e.g. User (posts_count) - Post where active = 1.

divdax avatar Aug 14 '17 12:08 divdax

@kirkbushell is this still somewhere on the roadmap? would you accept a PR?

jaulz avatar Mar 15 '21 18:03 jaulz

@jaulz I would.

kirkbushell avatar Oct 05 '21 02:10 kirkbushell