Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tl;dr;☹️ ☹️ 🤑☹️ ☹️ ☹️ 🤑
This PR introduces a new Lottery class. In isolation, this is not super useful, but when used with other features within Laravel and your own application, it can make for a nice experience.
Lotteries can be used in an assortment of ways, for example to do random sampling of an event or assign users to an A/B test group, etc.
A lottery may be created with 2 integer values or a single float.
It is also useful with built in framework features...
As the Lottery class is
callable
, it may be passed directly in as a handler to some of Laravel's features. The following is an example of its use with the newDB::whenQueryingForLongerThan
functionality.It could also be utilised with the existing n+1 reporting. This is especially useful because unlike the above function, the n+1 handler function may be called thousands of times per request...
You will notice in the above examples that we did not call
->choose()
. This is because, an previously mentioned, the Lottery iscallable
. Additionally you will notice that the closures passed to the lottery receive the arguments$model
and$relation
.I wrote a blog post introducing this idea of randomly sampling lazy loading violations via lottery after we implemented this at my
$previousGig
Full API examples
This new class could also be utilised in the framework where we perform lotteries, i.e. in the session and database lock garbage collection. I've not included that in this PR as I don't think we need to do it just for the sake of it. This is, after all, primarily a new user-facing feature.
I've also added some testing helpers similar to those found in other Laravel support classes.
Testing script