Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Http/Livewire/LivewireDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class LivewireDatatable extends Component
public $persistPerPage = true;
public $persistFilters = true;

public $tablePrefix = '';

/**
* @var array List your groups and the corresponding label (or translation) here.
* The label can be a i18n placeholder like 'app.my_string' and it will be automatically translated via __().
Expand Down Expand Up @@ -1008,6 +1010,8 @@ public function buildDatabaseQuery($export = false)
{
$this->query = $this->builder();

$this->tablePrefix = $this->query->getConnection()->getTablePrefix() ?? '';

$this->query->addSelect(
$this->getSelectStatements(true, $export)
->filter()
Expand Down Expand Up @@ -1130,10 +1134,10 @@ public function addGlobalSearch()
foreach ($this->getColumnFilterStatement($i) as $column) {
$query->when(is_array($column), function ($query) use ($search, $column) {
foreach ($column as $col) {
$query->orWhereRaw('LOWER(' . $col . ') like ?', '%' . mb_strtolower($search) . '%');
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $col . ') like ?', '%' . mb_strtolower($search) . '%');
}
}, function ($query) use ($search, $column) {
$query->orWhereRaw('LOWER(' . $column . ') like ?', '%' . mb_strtolower($search) . '%');
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', '%' . mb_strtolower($search) . '%');
});
}
});
Expand Down Expand Up @@ -1176,14 +1180,14 @@ public function addSelectFilters()
if ($this->freshColumns[$index]['type'] === 'json') {
$query->where(function ($query) use ($value, $index) {
foreach ($this->getColumnFilterStatement($index) as $column) {
$query->whereRaw('LOWER(' . $column . ') like ?', [mb_strtolower("%$value%")]);
$query->whereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', [mb_strtolower("%$value%")]);
}
});
} else {
$query->orWhere(function ($query) use ($value, $index) {
foreach ($this->getColumnFilterStatement($index) as $column) {
if (Str::contains(mb_strtolower($column), 'concat')) {
$query->orWhereRaw('LOWER(' . $column . ') like ?', [mb_strtolower("%$value%")]);
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', [mb_strtolower("%$value%")]);
} else {
$query->orWhereRaw($column . ' = ?', $value);
}
Expand Down Expand Up @@ -1251,7 +1255,7 @@ public function addTextFilters()
$query->orWhere(function ($query) use ($index, $value) {
foreach ($this->getColumnFilterStatement($index) as $column) {
$column = is_array($column) ? $column[0] : $column;
$query->orWhereRaw('LOWER(' . $column . ') like ?', [mb_strtolower("%$value%")]);
$query->orWhereRaw('LOWER(' . $this->tablePrefix . $column . ') like ?', [mb_strtolower("%$value%")]);
}
});
}
Expand Down