Skip to content

Commit 098fb1b

Browse files
thyseusHerbert Maschke
andauthored
obey table prefix, if defined in database connection (#365)
Co-authored-by: Herbert Maschke <thyseus@pm.me>
1 parent c991b15 commit 098fb1b

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Http/Livewire/LivewireDatatable.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class LivewireDatatable extends Component
7070
public $persistFilters = true;
7171
public $row = 1;
7272

73+
public $tablePrefix = '';
74+
7375
/**
7476
* @var array List your groups and the corresponding label (or translation) here.
7577
* The label can be a i18n placeholder like 'app.my_string' and it will be automatically translated via __().
@@ -1112,6 +1114,8 @@ public function buildDatabaseQuery($export = false)
11121114
{
11131115
$this->query = $this->builder();
11141116

1117+
$this->tablePrefix = $this->query->getConnection()->getTablePrefix() ?? '';
1118+
11151119
$this->query->addSelect(
11161120
$this->getSelectStatements(true, $export)
11171121
->filter()
@@ -1234,10 +1238,10 @@ public function addGlobalSearch()
12341238
foreach ($this->getColumnFilterStatement($i) as $column) {
12351239
$query->when(is_array($column), function ($query) use ($search, $column) {
12361240
foreach ($column as $col) {
1237-
$query->orWhereRaw('LOWER(' . $col . ') like ?', '%' . mb_strtolower($search) . '%');
1241+
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $col . ') like ?', '%' . mb_strtolower($search) . '%');
12381242
}
12391243
}, function ($query) use ($search, $column) {
1240-
$query->orWhereRaw('LOWER(' . $column . ') like ?', '%' . mb_strtolower($search) . '%');
1244+
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', '%' . mb_strtolower($search) . '%');
12411245
});
12421246
}
12431247
});
@@ -1280,14 +1284,14 @@ public function addSelectFilters()
12801284
if ($this->freshColumns[$index]['type'] === 'json') {
12811285
$query->where(function ($query) use ($value, $index) {
12821286
foreach ($this->getColumnFilterStatement($index) as $column) {
1283-
$query->whereRaw('LOWER(' . $column . ') like ?', [mb_strtolower("%$value%")]);
1287+
$query->whereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', [mb_strtolower("%$value%")]);
12841288
}
12851289
});
12861290
} else {
12871291
$query->orWhere(function ($query) use ($value, $index) {
12881292
foreach ($this->getColumnFilterStatement($index) as $column) {
12891293
if (Str::contains(mb_strtolower($column), 'concat')) {
1290-
$query->orWhereRaw('LOWER(' . $column . ') like ?', [mb_strtolower("%$value%")]);
1294+
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', [mb_strtolower("%$value%")]);
12911295
} else {
12921296
$query->orWhereRaw($column . ' = ?', $value);
12931297
}
@@ -1355,7 +1359,7 @@ public function addTextFilters()
13551359
$query->orWhere(function ($query) use ($index, $value) {
13561360
foreach ($this->getColumnFilterStatement($index) as $column) {
13571361
$column = is_array($column) ? $column[0] : $column;
1358-
$query->orWhereRaw('LOWER(' . $column . ') like ?', [mb_strtolower("%$value%")]);
1362+
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', [mb_strtolower("%$value%")]);
13591363
}
13601364
});
13611365
}

0 commit comments

Comments
 (0)