📅  最后修改于: 2023-12-03 15:17:12.161000             🧑  作者: Mango
Laravel Between is a package for Laravel that allows you to easily query for records that fall within a certain range.
You can install the package via Composer:
composer require djoudut/laravel-between
Then publish the configuration file:
php artisan vendor:publish --provider="Djoudut\Between\BetweenServiceProvider"
First, add the Between
trait to your model:
use Djoudut\Between\Traits\Between;
class YourModel extends Model
{
use Between;
// ...
}
This trait adds the between
scope to your model.
YourModel::between('created_at', '2022-01-01', '2022-01-31')->get();
This will return all the records that were created between January 1st and January 31st, 2022.
You can also use the orWhereBetween
scope to add additional conditions to your query:
YourModel::where('status', '=', 'active')->orWhereBetween('created_at', '2022-01-01', '2022-01-31')->get();
This will return all the active records, as well as the records that were created between January 1st and January 31st, 2022.
The configuration file allows you to customize the column names used for the timestamp fields:
return [
'fields' => [
'start' => 'start_date',
'end' => 'end_date',
],
];
By default, the fields are assumed to be named created_at
and updated_at
.
Laravel Between is a simple and powerful package that allows you to easily query for records within a certain range. It's a great addition to any Laravel project that needs to work with date and time ranges.