Skip to content

Commit 84b7ced

Browse files
authored
Support for Table prefixes (#26)
1 parent b415033 commit 84b7ced

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-cross-eloquent-search` will be documented in this file
44

5+
## 2.1.0 - 2021-08-09
6+
7+
- Support for Table prefixes
8+
59
## 2.0.4 - 2021-05-03
610

711
- Fix phpdoc comment format (credit to @gazben)

src/ModelToSearchThrough.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function getQualifiedColumns(): Collection
8282
*
8383
* @return \Illuminate\Database\Eloquent\Model
8484
*/
85-
protected function getModel(): Model
85+
public function getModel(): Model
8686
{
8787
return $this->builder->getModel();
8888
}

src/Searcher.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,10 @@ protected function makeSelects(ModelToSearchThrough $currentModel): array
337337
$qualifiedKeyName = $qualifiedOrderByColumnName = 'null';
338338

339339
if ($modelToSearchThrough === $currentModel) {
340-
$qualifiedKeyName = $modelToSearchThrough->getQualifiedKeyName();
341-
$qualifiedOrderByColumnName = $modelToSearchThrough->getQualifiedOrderByColumnName();
340+
$prefix = $modelToSearchThrough->getModel()->getConnection()->getTablePrefix();
341+
342+
$qualifiedKeyName = $prefix . $modelToSearchThrough->getQualifiedKeyName();
343+
$qualifiedOrderByColumnName = $prefix . $modelToSearchThrough->getQualifiedOrderByColumnName();
342344
}
343345

344346
return [

tests/SearchTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ public function it_can_count_the_results()
7171
$this->assertEquals(3, $count);
7272
}
7373

74+
/** @test */
75+
public function it_respects_table_prefixes()
76+
{
77+
$this->initDatabase('prefix');
78+
79+
$postA = Post::create(['title' => 'foo']);
80+
$postB = Post::create(['title' => 'bar']);
81+
$videoA = Video::create(['title' => 'foo']);
82+
$videoB = Video::create(['title' => 'bar', 'subtitle' => 'foo']);
83+
84+
$count = Search::add(Post::class, 'title')
85+
->add(Video::class, ['title', 'subtitle'])
86+
->count('foo');
87+
88+
$this->assertEquals(3, $count);
89+
}
90+
7491
/** @test */
7592
public function it_can_search_for_a_phrase()
7693
{

tests/TestCase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProtoneMedia\LaravelCrossEloquentSearch\Tests;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\DB;
67
use Orchestra\Testbench\TestCase as OrchestraTestCase;
78
use PDO;
89
use ProtoneMedia\LaravelCrossEloquentSearch\ServiceProvider;
@@ -22,6 +23,13 @@ public function setUp(): void
2223

2324
$this->app['config']->set('app.key', 'base64:yWa/ByhLC/GUvfToOuaPD7zDwB64qkc/QkaQOrT5IpE=');
2425

26+
$this->initDatabase();
27+
}
28+
29+
protected function initDatabase($prefix = '')
30+
{
31+
DB::purge('mysql');
32+
2533
$this->app['config']->set('database.connections.mysql', [
2634
'driver' => 'mysql',
2735
'url' => env('DATABASE_URL'),
@@ -33,7 +41,7 @@ public function setUp(): void
3341
'unix_socket' => env('DB_SOCKET', ''),
3442
'charset' => 'utf8mb4',
3543
'collation' => 'utf8mb4_unicode_ci',
36-
'prefix' => '',
44+
'prefix' => $prefix,
3745
'prefix_indexes' => true,
3846
'strict' => true,
3947
'engine' => null,

0 commit comments

Comments
 (0)