Skip to content

Commit

Permalink
Update FieldsContract.php
Browse files Browse the repository at this point in the history
  • Loading branch information
atmonshi committed Sep 12, 2024
1 parent e4a5c57 commit 53cf5dc
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use LaraZeus\Bolt\BoltPlugin;
use LaraZeus\Bolt\Concerns\HasHiddenOptions;
use LaraZeus\Bolt\Concerns\HasOptions;
use LaraZeus\Bolt\Contracts\Fields;
use LaraZeus\Bolt\Facades\Bolt;
use LaraZeus\Bolt\Models\Field;
use LaraZeus\Bolt\Models\FieldResponse;
use Illuminate\Support\Facades\Cache;
use LaraZeus\Bolt\Models\Response;
use LaraZeus\BoltPro\Models\Field as FieldPreset;

Expand Down Expand Up @@ -166,12 +166,12 @@ public function getCollectionsValuesForResponse(Field $field, FieldResponse $res

$response = Arr::wrap($response);

$dataSource = (int) $field->options['dataSource'] ?? $field->options['dataSource'];
$dataSource = (int) $field->options['dataSource'];
$cacheKey = 'dataSource_' . $dataSource . '_response_' . md5(serialize($response));

$response = Cache::remember($cacheKey, config('zeus-bolt.cache.collection_values') , function () use ($field, $response, $dataSource) {
$response = Cache::remember($cacheKey, config('zeus-bolt.cache.collection_values'), function () use ($field, $response, $dataSource) {

// Handle case when dataSource is not zero (new structure)
// Handle case when dataSource is from the default model: `Collection`
if ($dataSource !== 0) {
return BoltPlugin::getModel('Collection')::query()
->find($dataSource)
Expand All @@ -181,13 +181,17 @@ public function getCollectionsValuesForResponse(Field $field, FieldResponse $res
->join(', ') ?? '';
}

// Handle case when dataSource is zero (old structure)
$dataSourceClass = new $field->options['dataSource'];
// Handle case when dataSource is custom model class
if (class_exists($field->options['dataSource'])) {
$dataSourceClass = app($field->options['dataSource']);

return $dataSourceClass->getQuery()
->whereIn($dataSourceClass->getKeysUsing(), $response)
->pluck($dataSourceClass->getValuesUsing())
->join(', ');
return $dataSourceClass->getQuery()
->whereIn($dataSourceClass->getKeysUsing(), $response)
->pluck($dataSourceClass->getValuesUsing())
->join(', ');
}

return '';
});

return (is_array($response)) ? implode(', ', $response) : $response;
Expand Down

0 comments on commit 53cf5dc

Please sign in to comment.