userstable with a boolean flag in a
subscribedcolumn. Additionally, you want to see the oldest subscribers first. You keep track of when a user subscribed in a
subscribedDatecolumn. Your query might look as follows:
subscribedDatecolumn wasn't null?
scopekeyword. You call these functions without the
scopekeyword (as shown above).
query, a reference to the current
QuickBuilderinstance, as the first argument. Any other arguments passed to the scope will be passed in order after that.
QuickBuilderobject or nothing. Doing so lets you continue to chain methods on your Quick entity. If you instead return a value, Quick will pass on that value to your code. This lets you use scopes as shortcut methods that work on a query.
applyGlobalScopesmethod on an entity. Inside this entity you can call any number of scopes:
lastLoginDateof a user, not the entire
Loginrelationship. Subselects are perfect for this use case. You can even use subselects to provide the correct key for dynamic subselect relationships. We'll show how both work here.
lastLoginDatefor a User:
Userentity based on the value of the subselect, so long as we've called the scope adding the subselect first (or made it a global scope).
Userbased on the subselect:
addSubselecthelper method. Here is that function signature:
loginsrelationship? Or even
logins().latest().limit( 1 ).get()? Because that executes a second query. Using a subselect we get all the information we need in one query, no matter how many entities we are pulling back.
lastLoginDatefor a User using the existing relationship:
loginsrelationship is structured, we only have one place we need to change.
latestPostrelationship. This sequence will only execute two queries, no matter how many records are loaded.
CASEstatement as a field:
publishedStatusjust like any other attribute. It will not be updated, inserted, or saved though, as it is just a virtual column.
appendVirtualAttributemethod adds the given name as an attribute available in the entity.