Skip to content

Instantly share code, notes, and snippets.

@sawirricardo
Last active December 19, 2023 15:16
Show Gist options
  • Save sawirricardo/34c69a3476430369a53a3991338a20e8 to your computer and use it in GitHub Desktop.
Save sawirricardo/34c69a3476430369a53a3991338a20e8 to your computer and use it in GitHub Desktop.
HasPeriodColumns
<?php
namespace App\Support\Traits;
use Carbon\CarbonInterface;
use Illuminate\Contracts\Database\Eloquent\Builder;
trait HasPeriodColumns
{
public function scopeOverlapping(Builder $query, CarbonInterface $start, CarbonInterface $end, string $startColumn, string $endColumn): Builder
{
return $query->where(static function (Builder $query) use ($start, $end, $startColumn, $endColumn) {
return $query
->whereBetween($startColumn, [$start, $end])
->orWhereBetween($endColumn, [$start, $end])
->orWhere($startColumn, '>', $start)->where($endColumn, '<', $end);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment