Пакет Laravel Date Scopes надає деякі корисні області запитів для ваших моделей Laravel Eloquent. Це було натхненно цим твітом творця Livewire Калеба Порціо:
Чи було б радо, якби Eloquent мав це, так?
— Caleb Porzio (@calebporzio) April 7, 2023
pic.twitter.com/qjOD5vv68y Цей твіт надихнув статтю Laravel Eloquent Models: How to Filter by Dates like a Pro with Traits by Moumen Alisawe. Що потім надихнуло пакет Date Scopes.
Як було знайдено в readme, враховуючи наступну модель, додайте рису DateScopes
наступним чином:
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! 👏
Щоб почати, ви можете дізнатися більше про цей пакет, отримати повні інструкції щодо встановлення та переглянути вихідний код на GitHub.