Pakiet Laravel Data Scopes zawiera kilka przydatnych zakresów zapytań dla modeli Laravel Eloquent. Zainspirował go ten tweet twórcy Livewire Caleba Porzio:
Czy byłoby rad, gdyby Eloquent je miał, tak?
— Caleb Porzio (@calebporzio) April 7, 2023
.twitter.com/qjOD5vv68y Ten tweet zainspirował artykuł Laravel Eloquent Models: How to Filter by Dates like a Pro with Traits autorstwa Moumena Alisawe. Co następnie zainspirowało pakiet Date Scopes.
Jak można znaleźć w pliku readme, biorąc pod uwagę następujący model, dodaj cechę DateScopes
w następujący sposób:
use LaracraftTech\LaravelDateScopes\DateScopes;
class Transaction extends Model
{
use DateScopes;
}
Which gives you tons of time-based scopes. The following examples are for various hour scopes, and there are similar methods for seconds, minutes, days, weeks, months, years, decades, and so on:
// query transactions created during the last hour
Transaction::ofLastHour();
// query transactions created during the last 6 hours
Transaction::ofLast6Hours();
// query transactions created during the last 12 hours
Transaction::ofLast12Hours();
// query transactions created during the last 18 hours
Transaction::ofLast18Hours();
// query transactions created during the last 24 hours
Transaction::ofLast24Hours();
// query transactions created during the last N hours
Transaction::ofLastHours(48);
Here's an example of how to get all transactions in the last year using the lastYear()
date scope:
$transactions = Transaction::lastYear()->get();
I love how this idea was built upon through contributions of multiple people in the Laravel community. From inspiration, to example, to a package that you can easily install in your Laravel application! 👏
Aby rozpocząć, możesz dowiedzieć się więcej o tym pakiecie, uzyskać pełne instrukcje instalacji i wyświetlić kod źródłowy w witrynie GitHub.For get started, you can learn more about this package, get full installation instructions, and view the source code on GitHub.