From 7d31d5f83b30b7547e06840a3b8f6f8c860f47ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20=C4=8Cern=C3=BD?= Date: Sat, 30 Sep 2023 18:51:47 +0200 Subject: [PATCH] Update LaraGridServiceProvider.php --- src/Providers/LaraGridServiceProvider.php | 76 ++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/Providers/LaraGridServiceProvider.php b/src/Providers/LaraGridServiceProvider.php index 5c44657..296f1d3 100644 --- a/src/Providers/LaraGridServiceProvider.php +++ b/src/Providers/LaraGridServiceProvider.php @@ -2,19 +2,93 @@ namespace BoredProgrammers\LaraGrid\Providers; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Str; class LaraGridServiceProvider extends ServiceProvider { public function register(): void { - } public function boot(): void { $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'laragrid'); + + Builder::macro('whereLike', function ($attributes, $searchTerm) { + if ($searchTerm === null || $searchTerm === '') { + return $this; + } + + $this->whereOrWhereRelation($attributes, $searchTerm, 'LIKE', "%{$searchTerm}%"); + + return $this; + }); + + Builder::macro('whereEqual', function ($attributes, $value) { + if ($value === null || $value === '') { + return $this; + } + + $this->whereOrWhereRelation($attributes, $value); + + return $this; + }); + + Builder::macro('whereDateBetween', function ($attribute, $startDate = null, $endDate = null) { + if (!$startDate && !$endDate) { + return $this; + } + + $startDate = $startDate ?: Carbon::today(); + $endDate = $endDate ?: Carbon::today(); + + $this->where(function (Builder $query) use ($attribute, $startDate, $endDate) { + $query->when( + Str::contains($attribute, '.'), + function (Builder $query) use ($attribute, $startDate, $endDate) { + [$relationName, $relationAttribute] = explode('.', $attribute); + $query->orWhereHas( + $relationName, + function (Builder $query) use ($relationAttribute, $startDate, $endDate) { + $query->whereBetween($relationAttribute, [$startDate, $endDate]); + } + ); + }, + function (Builder $query) use ($attribute, $startDate, $endDate) { + $query->orWhereBetween($attribute, [$startDate, $endDate]); + } + ); + }); + + return $this; + }); + + Builder::macro('whereOrWhereRelation', function ($attributes, $value, $operator = '=', $formattedValue = null) { + $this->where(function (Builder $query) use ($attributes, $value, $operator, $formattedValue) { + foreach (Arr::wrap($attributes) as $attribute) { + $query->when( + Str::contains($attribute, '.'), + function (Builder $query) use ($attribute, $value, $operator, $formattedValue) { + [$relationName, $relationAttribute] = explode('.', $attribute); + $query->orWhereHas( + $relationName, + function (Builder $query) use ($relationAttribute, $value, $operator, $formattedValue) { + $query->where($relationAttribute, $operator, $formattedValue ?: $value); + } + ); + }, + function (Builder $query) use ($formattedValue, $attribute, $value, $operator) { + $query->orWhere($attribute, $operator, $formattedValue ?: $value); + } + ); + } + }); + }); } }