Skip to content
Merged
Show file tree
Hide file tree
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/Entries/EntryQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,15 @@ protected function getJsonCasts(): IlluminateCollection
$wheres = collect($this->builder->getQuery()->wheres);
$collectionWhere = $wheres->firstWhere('column', 'collection');

if (
! $collectionWhere
|| ! isset($collectionWhere['value'])
|| ! ($collection = Collection::find($collectionWhere['value']))
) {
if (! $collectionWhere) {
return collect([]);
}

if (isset($collectionWhere['values']) && count($collectionWhere['values']) == 1) {
$collectionWhere['value'] = $collectionWhere['values'][0];
}

if (! isset($collectionWhere['value']) || ! $collection = Collection::find($collectionWhere['value'])) {
return collect([]);
}

Expand Down
11 changes: 8 additions & 3 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,19 @@ public function entries_can_be_ordered_by_a_float_json_field()
Blueprint::shouldReceive('in')->with('collections/posts')->andReturn(collect(['posts' => $blueprint]));

Collection::make('posts')->save();
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'float' => 3.3])->create();
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'float' => 5.5])->create();
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'float' => 1.1])->create();
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'float' => '9.5'])->create();
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'float' => '10.2'])->create();
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'float' => '8.7'])->create();

$entries = Entry::query()->where('collection', 'posts')->orderBy('float', 'asc')->get();

$this->assertCount(3, $entries);
$this->assertEquals(['Post 3', 'Post 1', 'Post 2'], $entries->map->title->all());

$entries = Entry::query()->whereIn('collection', ['posts'])->orderBy('float', 'asc')->get();

$this->assertCount(3, $entries);
$this->assertEquals(['Post 3', 'Post 1', 'Post 2'], $entries->map->title->all());
}

#[Test]
Expand Down