Skip to content

Commit f5b883d

Browse files
committed
Added ability to get instance from class string
1 parent 820568e commit f5b883d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/SearchBuilder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ class SearchBuilder
2525
*/
2626
protected array $conditions = [];
2727

28-
public function __construct(Model $model)
28+
public function __construct(string|Model $model)
2929
{
30+
if (is_string($model)) {
31+
$model = new $model;
32+
}
33+
3034
$this->model = $model;
3135
}
3236

@@ -88,8 +92,7 @@ public function getQuery(): EloquentBuilder
8892

8993
return $this->query
9094
->withExpression('id_and_total_score', $this->getScoreQuery())
91-
->leftJoin('id_and_total_score', 'id_and_total_score.id', $table.'.id')
92-
->whereRaw($table.'.id in (select id from id_and_total_score)')
95+
->innerJoin('id_and_total_score', $table.'.id', 'id_and_total_score.id')
9396
->orderBy('score', 'desc');
9497
}
9598

tests/Unit/SearchBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
expect($builder)->toBeInstanceOf(SearchBuilder::class);
1010
});
1111

12+
test('a search builder can be instanciated with a string class name', function () {
13+
$builder = new SearchBuilder(FooModel::class);
14+
15+
expect($builder)->toBeInstanceOf(SearchBuilder::class);
16+
});
17+
1218
test('a model with the trait can get a configured search builder', function () {
1319
$builder = FooModel::searchBuilder();
1420

0 commit comments

Comments
 (0)