kirkbushell / eloquence

A drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
MIT License
537 stars 58 forks source link

can Countable support relation or condition ? #51

Closed gabrieltong closed 9 months ago

gabrieltong commented 8 years ago

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 commented 8 years ago

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 .

kirkbushell commented 8 years ago

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!

divdax commented 7 years ago

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

jaulz commented 3 years ago

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

kirkbushell commented 2 years ago

@jaulz I would.

kirkbushell commented 10 months ago

@gabrieltong this is solved in #107 - if you have time/moment, would be great to test it out and just double-check. But now, Eloquence support model relationships by default and works with them to drive the behaviours of the library.

kirkbushell commented 9 months ago

Closing as this will be available in the next big release for Eloquence.