Пакет 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.