Skip to content

Commit

Permalink
Merge pull request #277 from mohaphez/feature/add-getQuery
Browse files Browse the repository at this point in the history
[Feature] Add getQuery Method
  • Loading branch information
atmonshi authored Apr 21, 2024
2 parents 6eff433 + 130bba2 commit 0da74da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Contracts/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace LaraZeus\Bolt\Contracts;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

interface DataSource
{
public function title(): string;
Expand All @@ -13,4 +16,6 @@ public function getValuesUsing(): string;
public function getKeysUsing(): string;

public function getModel(): string;

public function getQuery(): Builder | Collection;
}
7 changes: 7 additions & 0 deletions src/DataSources/DataSourceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace LaraZeus\Bolt\DataSources;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use LaraZeus\Bolt\Contracts\DataSource;

abstract class DataSourceContract implements Arrayable, DataSource
Expand All @@ -14,6 +16,11 @@ public function getSort(): int
return 1;
}

public function getQuery(): Builder | Collection
{
return resolve($this->getModel())->query();
}

public function toArray(): array
{
return [
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function getCollectionsValuesForResponse(Field $field, FieldResponse $res
->join(', ');
} else {
$dataSourceClass = new $field->options['dataSource'];
$response = $dataSourceClass->getModel()::query()
$response = $dataSourceClass->getQuery()
->whereIn($dataSourceClass->getKeysUsing(), $response)
->pluck($dataSourceClass->getValuesUsing())
->join(', ');
Expand Down Expand Up @@ -218,7 +218,7 @@ public static function getFieldCollectionItemsList(Field | FieldPreset | array $
if (class_exists($zeusField->options['dataSource'])) {
//@phpstan-ignore-next-line
$dataSourceClass = new $zeusField->options['dataSource'];
$getCollection = $dataSourceClass->getModel()::pluck(
$getCollection = $dataSourceClass->getQuery()->pluck(
$dataSourceClass->getValuesUsing(),
$dataSourceClass->getKeysUsing()
);
Expand Down

0 comments on commit 0da74da

Please sign in to comment.