Skip to content

Commit

Permalink
Fixed postgres error
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Dec 4, 2024
1 parent d00891b commit 17e0483
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\DB;

class Category extends Model
{
Expand Down Expand Up @@ -65,21 +66,30 @@ public function setSlug()
$slug = $this->slug ?? $this->name;
$slug = \Str::slug($slug);

$regexOperators = [
'mysql' => 'RLIKE',
'pgsql' => '~',
'sqlite' => 'REGEXP'
];

$driver = DB::connection()->getDriverName();
$regexOperator = $regexOperators[$driver] ?? 'mysql';

if ($this->id) {
$similarSlugs = Category::where(function (Builder $q) use ($slug) {
$q->where('slug', '=', $slug)
->where('category_type_id', $this->category_type_id)
->where('id', '!=', $this->id);
})->where(function (Builder $q) use ($slug) {
})->where(function (Builder $q) use ($slug, $regexOperator) {
$q->where('id', '!=', $this->id)
->where('category_type_id', '!=', $this->category_type_id)
->orWhereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'");
->orWhereRaw("slug {$regexOperator} '^{$slug}(-[0-9]+)?$'");
})->select('slug')->get();
} else {
$similarSlugs = Category::where(function (Builder $q) use ($slug) {
$similarSlugs = Category::where(function (Builder $q) use ($slug, $regexOperator) {
$q->where('slug', '=', $slug)
->where('category_type_id', $this->category_type_id)
->orWhereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'");
->orWhereRaw("slug {$regexOperator} '^{$slug}(-[0-9]+)?$'");
})->select('slug')->get();
}

Expand Down

0 comments on commit 17e0483

Please sign in to comment.