Last active
December 19, 2023 15:16
-
-
Save sawirricardo/34c69a3476430369a53a3991338a20e8 to your computer and use it in GitHub Desktop.
HasPeriodColumns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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